C# Programming
Total Page:16
File Type:pdf, Size:1020Kb
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 © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. Intro-2 Course Contents Introduction and Overview Chapter 1 Overview and Development Environment Chapter 2 From Your Language to C# Chapter 3 User Interface Development Chapter 4 Defining User-Written Data Types Chapter 5 The .NET Framework Chapter 6 Interfaces and Polymorphism Chapter 7 Writing .NET Components Chapter 8 Accessing Databases Chapter 9 Other C# Features Chapter 10 Course Summary Next Steps © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. Intro-3 Course Contents Appendix A Performance Tips Appendix B Unsafe Features Appendix C Other C# Features © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. Intro-4 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 SQL = structured query language HTML = hypertext markup language T-SQL = Transact-SQL PL/SQL = procedural language/structured query language VB = Visual Basic © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. Intro-5 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 Courier is program output or C# code • Text in Courier italic is nonliteral code or command text Slippery When Wet Question Potentially confusing or variance from Question for the attendee common use in other languages Surfer Warning Additional information can be found Feature that if used incorrectly can lead to at this web reference problems Version n Crystal Ball This feature is available only in C# Reference to material that will be discussed version n or later. For example, the later note below would indicate that the Good Idea capability discussed requires at least C# V6 Feature to make programs easier to write or maintain © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. Intro-6 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 © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. 1-2 Chapter Contents Evolution From C to C# • Development Environment • Hands-On Exercise 1.1 • Review Questions © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. 1-3 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 © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. 1-4 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., main and printf #include <stdio.h> int main(void) { printf("Hello world!\n"); return 0; } © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. 1-5 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 int main(void) main( void { ) { printf printf("Hello World\n"); ( return 0; "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 © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. 1-6 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 … © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. 1-7 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) © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. 1-8 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 ISO = International Organization for IEC = International Electrotechnical Commission Standardization © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. 1-9 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 • Main method (function) is in a class • Uses Console.Write method from the .NET framework library • Otherwise, it has mostly the same tokens and keywords C:\Course\419\Samples\HelloWorldExample © Learning Tree International, Inc. All rights reserved. Not to be reproduced without prior written consent. 1-10 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. */