Module 2 98
Assignment 1 Introduce C# as Object Oriented programming language. Explain, tokens, lexicals and control flow constructs. 99
The C# Family Tree C Platform Independence C++ Object Orientation Java Platform Independence + Component Orientation VB Elegance C# Productivity 100
Introduction ti C# is intended to be a simple, modern, general-purpose, type-safe, object-oriented programming language. Its development team is led by Anders Hejlsberg. The most recent version is C# 4.0, which was released on April 12, 2010. 101
C# : The favourite programming language of.net Platform Paradigm Multi-Paradigm: Structured, Object-oriented, Event- Driven, Functional Appeared in 2001 Designed by Microsoft Developer Microsoft Stable release 4.0 (April 12, 2010) Influenced by Java, C++, Eiffel Platform Common Language Infrastructure License CLR Proprietary File extensions.cs Official Help C# Language (MSDN Microsoft Develpers Network) 102
The name "C Sharp" was inspired by musical notation where a sharp indicates that the written note should be made a half-step higher in pitch. This is similar to the language name of C++, where "++ indicates that a variable should be incremented by 1. Source: http://en.wikipedia.org/wiki/c_sharp_(programming_language) 103
VERSIONS of C# Language Version Date.NET Framework Visual Studio C# 1.0 C# 1.2 C# 2.0 C# 3.0 C# 4.0 January 2002.NET Framework 1.0 Visual Studio.NET 2002 Visual Studio April 2003.NET Framework 1.11.NET 2003 November 2005.NET Framework 2.0 November 2007.NET Framework 3.5 Visual Studio.NET 2005 Visual Studio.NET 2008 Visual Studio April 2010.NET Framework 4.0.NET 2010 104
Summary of Versions C# 2.0 C# 3.0 C# 4.0 C# 5.0 (planned) Features added 1. Generics 1. Implicitly typed 2. Partial types variables 3. Anonymous 2. Implicitly itl typed methods arrays 4. Iterators 3. Anonymous types 5. Nullable 4. Extension methods types 5. Query expressions 6. Lambda expressions 7. Expression trees 1. Dynamic binding 2. Named and optional arguments 3. Generic co- and contra variance 1. Asynchronou s methods 2. Compiler As a Service 105
C# : A Curtain Raiser. C# is a modern language. g It supports the notion of data types, flow of control statements, operators, arrays, properties, and exceptions. C# is an object-oriented language. It supports classes and the object-oriented nature of classes including encapsulation, inheritance, and polymorphism. C# also supports interfaces in conjunction with the.net Common Language g Runtime (CLR) garbage g collection, which some feel is necessary in an object-oriented language. It also supports the notion of indexers, which in simplified terms lets you manipulate objects as arrays and delegates. 106
C# Versus Java Both new-generation languages g Both provide automatic garbage collection, reducing low-level maintenance tasks from the programmer Syntactically almost similar Concept of intermediate language common C# supports more primitive data types than Java, and also allows more extension to the value types like enumerations & structures. C# permits operator overloading Like Java, C# does not support multiple implementation inheritance A class cannot be derived from more than one class However, a class can be derived from multiple interfaces. C# uses delegates type-safe method pointers to implement event handling. In addition to jagged arrays, C# also implements genuine rectangular array, as against Java, where multidimensional arrays are implemented solely with single-dimensional array. 107
C# Versus C++ Closest relative, than java & vb. No need of header files. All code inline..net runtime provides memory management activities like garbage collection, importance of pointer nullifies. C# types are ultimately derived from the object type. Automatic array bounds checking. Fall through behavior of C++ Switch statement is disallowed in C#. C# gives upon the idea of multiple inheritance. Support for class properties of the kind found in VB Class methods are called using the. Operator rather than the :: operator. 108
Features of C# Simplicity Consistent type behavior supported by a unified type system Automatic garbage collection Error handling features Modern debugging features Robust security features Pure OOPL, everything is an object. Type safety. Automatic bound-check for collections. Versioning support interoperable 109
Boxing and unboxing Boxing is the operation of converting a value of a value type into a value of a corresponding reference type.boxing in C# is implicit. Unboxing is the operation of converting a value of a reference type (previously boxed) into a value of a value type.unboxing in C# requires an explicit type cast. A boxed object of type T can only be unboxed to a T (or a nullable T). Example: int foo = 42; // Value type. object bar = foo; // foo is boxed to bar. int foo2 = (int)bar; // Unboxed back to value type. On the stack i On the heap 123 On the stack i On the heap int i = 123; o (i boxed) int int i = 123; o Object o = i; int 123 Object o = i; j 123 int j = (int) o; 123 110
Tokens token examples identifiers Hiker Main result keywords public class int literals 9 6 13 operators + - * / % = punctuators ; { }, 111
Identifiers Rules answer42 42Answer different Different made of letters and digits must start with a letter underscore is a letter case sensitive No other special character other than underscore. The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style. 112
Valid identifiers _minute total Sum4 Hello67_hello @class _ 34343 113
Invalid Identifiers &total Minute# 4hour @12hello (strongly discouraged) abc@abc Sum terms $asdfa 114
Keywords abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void while get set value add remove These are sometimes reserved Verbatim: Any keyword can be used as an identifier if preceded by @ 115
Data Types C# is a type-safe language. Variables are declared as being of a particular type, and each variable is constrained to hold only values of its declared type. Value types [Stack] Primitives int i; Enum enum state {off, on} Struct struct point { int x; float y;} Nullable Types Reference types [Heap] Predefined d Reference Types Object String User-defined Reference Types Classes Interfaces Delegates Arrays 116
A Value-Type variable directly contains an object with some value. No other variable can directly contain the object contained. Reference type variable directly contains is something which refers to an object. Another variable can contain a reference to the same object referred to by the original variable. Reference types actually hold the value of a memory address occupied by the object they reference. Class Demo { Class XYZ { Public int myvalue; } Public static void Main() { XYZ X = newxyz(); X.myValue = 10; XYZ Z = X; Z.myValue = 20; // after this statement both X.myValue and Z.myValue equals 20. } } 117
Literals truth type bool literal true false floating point integers double 3.1415 int 9 6 13 42 characters sequences of characters char string 'X' "Hello" nothing object null 118
Operators symbol category + - * / % arithmetic ==!= equality < <= > >= relational &&! logical = assignment & ^ ~ << >> bitwisei 119
Punctuators t some tokens that group or separate { and } form scoped blocks semi-colons mark the end of a simple statement using System; public class Hiker { public static void Main() { int result; result = 9 * 6; int tirteen; tirteen = 13; Console.Write(result / tirteen); Console.Write(result % tirteen); } } 120