Course 419
C# Programming
G419/CN/E.1/609/D.1
© LEARNING TREE INTERNATIONAL, INC.
All rights reserved.
All trademarked product and company names are the property of their respective trademark holders.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, or translated into any language, without the prior written permission of the publisher.
Copying software used in this course is prohibited without the express permission of Learning Tree International, Inc. Making unauthorized copies of such software violates federal copyright law, which includes both civil and criminal penalties.
Introduction and Overview
Course Objectives
In this course, we will discuss the syntax and semantics of C# as
• An object-oriented language
◦ Using encapsulation, inheritance, interfaces, realization, polymorphism, associations, and aggregates
• A component-oriented language focusing on reuse via the .NET Framework
Library, including
◦ Desktop graphical and web-based user interfaces ◦ Enabling cross-language operation ◦ Performing I/O using serial streams and serialization ◦ Interfacing to legacy COM components
• A data-oriented language
◦ Using LINQ and the Entity Framework
COM = component object model I/O = input/output LINQ = Language Integrated Query
Intro-2
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Course Contents
Introduction and Overview
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10
Overview and Development Environment From Your Language to C# User Interface Development Defining User-Written Data Types The .NET Framework Interfaces and Polymorphism Writing .NET Components Accessing Databases Other C# Features Course Summary Next Steps
Intro-3
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Course Contents
Appendix A Appendix B Appendix C
Performance Tips Unsafe Features Other C# Features
Intro-4
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Prerequisites
Although this course is an introduction to C#, it is intended for
experienced programmers
• A background in a modern procedural language is needed
◦ For example: C/C++, Java, VB, VB.NET, Pascal
• You will need to be comfortably familiar with concepts such as
◦ Data types, variables, declarations, conditionals, loops, expressions, functions, parameter passing, and procedural language flow
Course 502, Programming With .NET Introduction, is recommended if you have only
• HTML or SQL experience • Experience in languages like COBOL, RPG, PL/SQL, T-SQL
Some modest exposure to object-oriented concepts is also assumed
Please notify your instructor if you do not meet the prerequisites
COBOL = common business-oriented language HTML = hypertext markup language PL/SQL = procedural language/structured query language
SQL = structured query language T-SQL = Transact-SQL VB = Visual Basic
Intro-5
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Course Fonts and Icons
These fonts and symbols have special meaning in the Course Notes:
• Text in Century Schoolbook italic introduces a new concept or term • Text in Courier bold is C# code keywords or command text • Text in plain Courieris program output or C# code • Text in Courier italic is nonliteral code or command text
Slippery When Wet
Question
Potentially confusing or variance from common use in other languages
Question for the attendee
Surfer
Warning
Additional information can be found at this web reference
Feature that if used incorrectly can lead to problems
Version n
Crystal Ball
This feature is available only in C# version n or later. For example, the note below would indicate that the capability discussed requires at least C# V6
Reference to material that will be discussed later
Good Idea
Feature to make programs easier to write or maintain
Intro-6
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Chapter 1
Overview and Development
Environment
Chapter Objectives
In this chapter, we will examine A brief history of C# Some elementary features of the language How to use our development tools
1-2
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Chapter Contents
Evolution From C to C#
• Development Environment • Hands-On Exercise 1.1 • Review Questions
1-3
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Differences and Similarities
C# has borrowed many concepts and constructs from other languages
• In particular, C++, Java, VB, and SQL
Depending on your background, some aspects of C# might seem the same as a language you have previously used, but …
Don’t be fooled!
• In the midst of the familiar, there will often be some unexpected differences
The course is written with these differences and similarities in mind
1-4
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
C-Style Syntax
It all started with C
• Dennis Ritchie at Bell Labs in 1971 • The language of choice on UNIX and PCs in the 1970s and 1980s
◦ Still widely used
C introduced what is now called C-style syntax
• Each statement ends in a semicolon (;) • Code blocks (body) are enclosed between {and } • Uppercase and lowercase are distinct • Function-oriented; e.g., mainand printf
#include <stdio.h> int main(void)
{printf("Hello world!\n");
return 0;
}
1-5
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
C-Style Syntax
C-style syntax is free-format
• Input is broken into tokens by the compiler • The order of the tokens is important, but not their column positions
For example, these programs are semantically identical:
int
main(
int main(void)
{
void
) { printf ("Hello World\n")
; return
0; } printf("Hello World\n");
return 0;
}
int main(void) {printf("Hello World\n"); return 0;}
Good engineering practice suggests adopting a clear, properly indented, consistent coding style
• This course demonstrates the generally accepted Microsoft coding style
1-6
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Evolution From C
The C language eventually evolved into other languages with similar syntax, such as C++
• Which added object orientation
… and then Java
• Which was more portable and easier to program than C++
◦ But with reduced performance
… and then JavaScript (JScript)
• For scripting and web niches
… and then …
1-7
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
C# History
C# appeared in 2001 as the “first 21st-century language”
• Developed by Microsoft for its .NET initiative • Follows the C/C++/Java trend
◦ But also borrows from Visual Basic, Smalltalk, Lisp, and others
Designed to be
• Easier to write and much less error-prone than C++ • More efficient with better performance than Java • Multimission—to be used for virtually any kind of application • Multilayer—to be the language of choice in any application layer
C# is
• Object-Oriented (OO), supporting all modern OO concepts • Component-Oriented (CO), with CO features built into the language • Data-oriented, with SQL-like data-manipulation built into the language
◦ Called Language INtegrated Query (LINQ)
1-8
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
C# Versions and Standards
C# is not Microsoft’s language per se
• Standardized through ECMA and ISO/IEC
Microsoft version is called Visual C# .NET®
• February 2002: V1—pre-standard version had most language features
• November 2005: V2—fully compliant with 23270 1st edition • December 2007: V3—fully compliant with ECMA-344 4th edition • April 2010: V4—fully compliant, adds extended features • August 2012: V5—fully compliant, adds extended features • July 2015: V6—fully compliant, adds numerous small, very useful features
ECMA = European Computer Manufacturers Association IEC = International Electrotechnical Commission
ISO = International Organization for Standardization
1-9
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Hello World in C#
Here is the “Hello World” program in C#:
using System; public class HelloWorld
{
public static int Main()
{
Console.Write("Hello world!\n");
return 0;
}
}
Syntax is similar and output is identical to previous C program
• Mainmethod (function) is in a class
• Uses Console.Writemethod from the .NET framework library • Otherwise, it has mostly the same tokens and keywords
C:\Course\419\Samples\HelloWorldExample
1-10
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.
Comments
In a C# program, only comments are not tokenized
• Considered whitespace by the compiler
There are three different styles
• Multiline starts with /*and ends with */ • Single-line starts with //and finishes at the end of the line
• XML-based documentation uses ///
///<summary> /// This will be included in the documentation and IntelliSense ///</summary>
public static int Main()
{/* This style of comment can go over over multiple lines. */
int i = 100; // This style goes to the end of the line.
}
XML = extensible markup language
1-11
© Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent.