200 likes | 380 Views
C# Language Report. By Trevor Adams. Language History. Developed by Microsoft Principal Software Architect Anders Hejlsberg C# 1.0 – mid 2000 C# 2.0 – 2005 C# 3.0 – Currently Under Development. Implementation. Hybrid Implementation .Net Framework Preprocessor Unsafe Code.
E N D
C# Language Report By Trevor Adams
Language History • Developed by Microsoft • Principal Software Architect • Anders Hejlsberg • C# 1.0 – mid 2000 • C# 2.0 – 2005 • C# 3.0 – Currently Under Development
Implementation • Hybrid Implementation • .Net Framework • Preprocessor • Unsafe Code
Overall Structure of a Program // A skeleton of a C# program using System; namespace MyNamespace1 { class MyClass1 { } struct MyStruct { } interface IMyInterface { } delegate int MyDelegate(); enum MyEnum { } namespace MyNamespace2 { } class MyClass2 { public static void Main(string[] args) { } } }
Control Structures • Selection Statements • Iterative Statements • Unconditional Branching
Selection Statements selection-statement=> if-statement |switch-statement if (x == y) { //execute block if true} else { //execute block if false} string myString = "hello"; switch (myString) { case "hello": Console.WriteLine("Hello there"); break; case "goodbye": Console.WriteLine(“Good Bye Dude"); break; }
Iterative Statements iteration-statement => while-statement | do-statement | for-statement | foreach-statement using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace ConApp.Demo { class Program { static void Main(string[] args) { ArrayList arrList = new ArrayList(); arrList.Add("First Item"); arrList.Add("Second Item"); arrList.Add("Third Item"); foreach(string myString in arrList){ Console.WriteLine(myString); } Console.ReadLine(); } } }
Unconditional Branching jump-statement => break-statement | continue-statement | goto-statement | return-statement | throw-statement
Data Types • Primitive Data Types • Arrays • Record Types • Union Types • Pointer Types
Primitive Data Types • object - The ultimate base type of all other types • string - String type; a string is a sequence of Unicode characters • sbyte - 8-bit signed integral type • short - 16-bit signed integral type • int - 32-bit signed integral type • long - 64-bit signed integral type l • byte - 8-bit unsigned integral type • ushort - 16-bit unsigned integral type • uint - 32-bit unsigned integral type • ulong - 64-bit unsigned integral type • float - Single-precision floating point type • double - Double-precision floating point type • bool - Boolean type; a bool value is either true or false • char - Character type; a char value is a Unicode character • decimal - Precise decimal type with 28 significant digits
Arrays int[] arr = new int[] {1, 2, 3, 4, 5}; int[,] a2 = new int[,] {{1, 2, 3}, {4, 5, 6}}; int[,,] a3 = new int[10, 20, 30]; int[][] j2 = new int[3][]; j2[0] = new int[] {1, 2, 3}; j2[1] = new int[] {1, 2, 3, 4, 5, 6}; j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
Record Types struct Point { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; } }
Union Types [StructLayout(LayoutKind.Explicit)] public struct MyUnion { [FieldOffset(0)] public int x; [FieldOffset(0)] public double y; }
Pointer Types using System;class MyClass{ public unsafe void Method() { int x = 10; int y = 20; int *ptr1 = &x; int *ptr2 = &y; Console.WriteLine((int)ptr1); Console.WriteLine((int)ptr2); Console.WriteLine(*ptr1); Console.WriteLine(*ptr2); }}
Sub Programs and Parameter Passing • The way variables are passed is affected by two things: • The data type of the parameter. • What modifiers are present when the parameter is passed.
Value vs. Reference Types • The following types are passed by Value by default: • Struct types • Enumeration types • Numeric types • Integral types • Floating-point types • decimal • bool
Parameter Modifiers • ref – allows a value type to be passed by reference • out – allows a parameter to be passed as an out parameter
Parameter Modifiers class ParameterModifiers { static void Main(string[] args) { int myInt = 9; modifyInt1(myInt); Console.WriteLine(myInt); modifyInt2(ref myInt); Console.WriteLine(myInt); modifyInt3(out myInt); Console.WriteLine(myInt); Console.ReadLine(); } private static void modifyInt1(int myInt) { myInt++; } public static void modifyInt2(ref int myInt) { myInt++; } private static void modifyInt3(out int myInt) { myInt++; } }