C# Language Report By Trevor Adams
Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect Anders Hejlsberg Anders Hejlsberg C# 1.0 – mid 2000 C# 1.0 – mid 2000 C# 2.0 – 2005 C# 2.0 – 2005 C# 3.0 – Currently Under Development C# 3.0 – Currently Under Development
Implementation Hybrid Implementation Hybrid Implementation.Net Framework.Net Framework Preprocessor Preprocessor Unsafe Code Unsafe Code
Overall Structure of a Program // A skeleton of a C# program using System; namespace MyNamespace1 { class MyClass1 class MyClass1 { } struct MyStruct struct MyStruct { } interface IMyInterface interface IMyInterface { } delegate int MyDelegate(); delegate int MyDelegate(); enum MyEnum enum MyEnum { } namespace MyNamespace2 namespace MyNamespace2 { } class MyClass2 class MyClass2 { public static void Main(string[] args) public static void Main(string[] args) { } }}
Control Structures Selection Statements Selection Statements Iterative Statements Iterative Statements Unconditional Branching 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 { class Program { static void Main(string[] args) { static void Main(string[] args) { ArrayList arrList = new ArrayList(); ArrayList arrList = new ArrayList(); arrList.Add("First Item"); arrList.Add("First Item"); arrList.Add("Second Item"); arrList.Add("Second Item"); arrList.Add("Third Item"); arrList.Add("Third Item"); foreach(string myString in arrList){ foreach(string myString in arrList){ Console.WriteLine(myString); Console.WriteLine(myString); } Console.ReadLine(); Console.ReadLine(); } }}
Unconditional Branching jump-statement => break-statement | continue-statement | goto-statement | return-statement | throw-statement
Data Types Primitive Data Types Primitive Data Types Arrays Arrays Record Types Record Types Union Types Union Types Pointer Types Pointer Types
Primitive Data Types object - The ultimate base type of all other types object - The ultimate base type of all other types string - String type; a string is a sequence of Unicode characters string - String type; a string is a sequence of Unicode characters sbyte - 8-bit signed integral type sbyte - 8-bit signed integral type short - 16-bit signed integral type short - 16-bit signed integral type int - 32-bit signed integral type int - 32-bit signed integral type long - 64-bit signed integral type l long - 64-bit signed integral type l byte - 8-bit unsigned integral type byte - 8-bit unsigned integral type ushort - 16-bit unsigned integral type ushort - 16-bit unsigned integral type uint - 32-bit unsigned integral type uint - 32-bit unsigned integral type ulong - 64-bit unsigned integral type ulong - 64-bit unsigned integral type float - Single-precision floating point type float - Single-precision floating point type double - Double-precision floating point type double - Double-precision floating point type bool - Boolean type; a bool value is either true or false bool - Boolean type; a bool value is either true or false char - Character type; a char value is a Unicode character char - Character type; a char value is a Unicode character decimal - Precise decimal type with 28 significant digits 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 int x, y; public Point(int x, int y) { public Point(int x, int y) { this.x = x; this.x = x; this.y = y; 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 way variables are passed is affected by two things: The data type of the parameter. The data type of the parameter. What modifiers are present when the parameter is passed. What modifiers are present when the parameter is passed.
Value vs. Reference Types The following types are passed by Value by default: The following types are passed by Value by default: Struct types Struct types Enumeration types Enumeration types Numeric types Numeric types Integral types Integral types Floating-point types Floating-point types decimal decimal bool bool
Parameter Modifiers ref – allows a value type to be passed by reference ref – allows a value type to be passed by reference out – allows a parameter to be passed as an out parameter out – allows a parameter to be passed as an out parameter
Parameter Modifiers class ParameterModifiers { static void Main(string[] args) { static void Main(string[] args) { int myInt = 9; int myInt = 9; modifyInt1(myInt); modifyInt1(myInt); Console.WriteLine(myInt); Console.WriteLine(myInt); modifyInt2(ref myInt); modifyInt2(ref myInt); Console.WriteLine(myInt); Console.WriteLine(myInt); modifyInt3(out myInt); modifyInt3(out myInt); Console.WriteLine(myInt); Console.WriteLine(myInt); Console.ReadLine(); Console.ReadLine(); } private static void modifyInt1(int myInt) { private static void modifyInt1(int myInt) { myInt++; myInt++; } public static void modifyInt2(ref int myInt) { public static void modifyInt2(ref int myInt) { myInt++; myInt++; } private static void modifyInt3(out int myInt) { private static void modifyInt3(out int myInt) { myInt++; myInt++; } }