Lecture notes for C# and .net framework

how c# is different from java and how c# and .net are interrelated. And how c# compiler works pdf free download
Dr.JamesSmith Profile Pic
Dr.JamesSmith,France,Professional
Published Date:11-07-2017
Your Website URL(Optional)
Comment
Introduction to C The New Language for . H.Mössenböck University of Linz, Austria moessenboeckssw.uni-linz.ac.atContents Advanced C Introduction to C 1. Overview 7. Inheritance 2. Types 8. Interfaces 3. Expressions 9. Delegates 4. Declarations 10. Exceptions 5. Statements 11. Namespaces and Assemblies 6. Classes and Structs 12. Attributes 13. Threads 14. XML Comments References: • B.Albahari, P.Drayton, B.Merrill: C Essentials. O'Reilly, 2001 • S.Robinson et al: Professional C, Wrox Press, 2001 • Online documentation on the .NET SDK CD 2Features of C Very similar to Java 70% Java, 10% C++, 5% Visual Basic, 15% new As in Java As in C++ • Object-orientation (single inheritance) • (Operator) Overloading • Interfaces • Pointer arithmetic in unsafe code • Exceptions • Some syntactic details • Threads • Namespaces (like Packages) • Strong typing • Garbage Collection • Reflection • Dynamic loading of code • ... 3New Features in C Really new (compared to Java) "Syntactic Sugar" • Reference and output parameters • Component-based programming • Objects on the stack (structs) - Properties • Rectangular arrays - Events • Enumerations • Delegates • Unified type system • Indexers • goto • Operator overloading • Versioning • foreach statements • Boxing/unboxing • Attributes • ... 4Hello World File Hello.cs using System; • uses the namespace System • entry point must be called Main class Hello • output goes to the console • file name and class name static void Main() need not be identical Console.WriteLine("Hello World"); Compilation (in the Console window) csc Hello.cs Execution Hello 5Structure of C Programs Programm File F1.cs File F2.cs File F3.cs namespace A ... namespace B ... namespace C ... class X ... class Y ... class Z ... • If no namespace is specified = anonymous default namespace • Namespaces may also contain structs, interfaces, delegates and enums • Namespace may be "reopened" in other files • Simplest case: single class, single file, default namespace 6A Program Consisting of 2 Files Counter.cs class Counter Compilation int val = 0; csc Counter.cs Prog.cs public void Add (int x) val = val + x; = generates Prog.exe public int Val () return val; Execution Prog Prog.cs using System; class Prog Working with DLLs static void Main() csc /target:library Counter.cs Counter c = new Counter(); = generates Counter.dll c.Add(3); c.Add(5); Console.WriteLine("val = " + c.Val()); csc /reference:Counter.dll Prog.cs = generates Prog.exe 7TypesUnified Type System Types Value Types Reference Types Pointers Simple Types Enums Structs Classes Interfaces Arrays Delegates bool sbyte byte float char short ushort double int uint decimal User-defined Types long ulong All types are compatible with object - can be assigned to variables of type object - all operations of type object are applicable to them 9Value Types versus Reference Types Value Types Reference Types variable contains value reference stored on stack heap initialisation 0, false, '\0' null assignment copies the value copies the reference example int i = 17; string s = "Hello"; int j = i; string s1 = s; i 17 s H e l l o j 17 s1 10Simple Types Long Form in Java Range sbyte System.SByte byte -128 .. 127 byte System.Byte - 0 .. 255 short System.Int16 short -32768 .. 32767 ushort System.UInt16 - 0 .. 65535 int System.Int32 int -2147483648 .. 2147483647 uint System.UInt32 - 0 .. 4294967295 63 63 long System.Int64 long -2 .. 2 -1 64 ulong System.UInt64 - 0 .. 2 -1 float System.Single float ±1.5E-45 .. ±3.4E38 (32 Bit) double System.Double double ±5E-324 .. ±1.7E308 (64 Bit) decimal System.Decimal - ±1E-28 .. ±7.9E28 (128 Bit) bool System.Boolean boolean true, false char System.Char char Unicode character 11Compatibility Between Simple Types decimal double float long int short sbyte ulong uint ushort byte only with type cast char 12Enumerations List of named constants Declaration (directly in a namespace) enum Color red, blue, green // values: 0, 1, 2 enum Access personal=1, group=2, all=4 enum Access1 : byte personal=1, group=2, all=4 Use Color c = Color.blue; // enumeration constants must be qualified Access a = Access.personal Access.group; if ((Access.personal & a) = 0) Console.WriteLine("access granted"); 13Operations on Enumerations Compare if (c == Color.red) ... if (c Color.red && c = Color.green) ... +, - c = c + 2; ++, c++; & if ((c & Color.red) == 0) ... c = c Color.blue; c = Color.red; The compiler does not check if the result is a valid enumeration value. Note - Enumerations cannot be assigned to int (except after a type cast). - Enumeration types inherit from object (Equals, ToString, ...). - Class System.Enum provides operations on enumerations (GetName, Format, GetValues, ...). 14Arrays One-dimensional Arrays int a = new int3; int b = new int 3, 4, 5; int c = 3, 4, 5; SomeClass d = new SomeClass10; // Array of references SomeStruct e = new SomeStruct10; // Array of values (directly in the array) int len = a.Length; // number of elements in a 15Multidimensional Arrays a01 Jagged (like in Java) a int a = new int2; a0 a0 = new int3; a1 a1 = new int4; int x = a01; int len = a.Length; // 2 len = a0.Length; // 3 Rectangular (more compact, more efficient access) a0, 1 int, a = new int2, 3; a int x = a0, 1; int len = a.Length; // 6 len = a.GetLength(0); // 2 len = a.GetLength(1); // 3 16Class System.String Can be used as standard type string string s = "Alfonso"; Note • Strings are immutable (use StringBuilder if you want to modify strings) • Can be concatenated with +: "Don " + s • Can be indexed: si • String length: s.Length • Strings are reference types = reference semantics in assignments • but their values can be compared with == and = : if (s == "Alfonso") ... • Class String defines many useful operations: CompareTo, IndexOf, StartsWith, Substring, ... 17Structs Declaration struct Point public int x, y; // fields public Point (int x, int y) this.x = x; this.y = y; // constructor public void MoveTo (int a, int b) x = a; y = b; // methods Use Point p = new Point(3, 4); // constructor initializes object on the stack p.MoveTo(10, 20); // method call 18Classes Declaration class Rectangle Point origin; public int width, height; public Rectangle() origin = new Point(0,0); width = height = 0; public Rectangle (Point p, int w, int h) origin = p; width = w; height = h; public void MoveTo (Point p) origin = p; Use Rectangle r = new Rectangle(new Point(10, 20), 5, 5); int area = r.width r.height; r.MoveTo(new Point(3, 3)); 19Differences Between Classes and Structs Classes Structs Reference Types Value Types (objects stored on the heap) (objects stored on the stack) support inheritance no inheritance (all classes are derived from object) (but compatible with object) can implement interfaces can implement interfaces may have a destructor no destructors allowed 20

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.