• Common Language Specifications (CLS) → Common Type Systems (CTS)

  • CTS →
    • Value Types
    • Reference Types

  • Value Type →

    • Struct
    • Enum
    • All Built-In data types are value data types expect (String - System.Object)
    • Any value type allocated in Stack.
    • int x; → allocation of 4 uninitialized bytes in stack.
    • int = Int32
    • Int32 → base class library (BCL) for int
    • y = x; → copy the value of x and assign it to y
    • ++x; → does not affect y after assignment

    1.png

    int x;
    ///allocation 4 uninitialized bytes in stack
    ///int : c# keyword
    
    x = 5;
    
    int32 y;
    /// int64 bcl struct
    ///allocation 4 uninitialized bytes in stack
    
    y = x;
    ++x;
    
    console.writeline(x);
    console.writeline(y);
    

  • Reference Type →

    • Class - String - System.Object
    • System.Object → all data types inherits from this class for 2 reasons:
      • generics was invented in 2005 so from 2002 to 2005 System.Object was used for every thing.
      • set of behaviors should be supported in all Data Types:
        • Public Virtual String ToString(); → return object state (values) in string form
        • Public Virtual int GetHashCode(); -> return object identity (unique identifier)
        • public virtual bool Equals (Object O2); -> to check equality
        • public type GetType(); -> return data type
    • Object O1; → zero bytes have been allocated in Heap.
    • O1 = new Object();
      • new (IL -> newOBJ) →
        • allocate required numbers of bytes in Heap (Object Size + CLR Overhead Variables)
        • initialize (cross out) allocated byte
        • call constructor if exits
        • assign reference to new allocated object
    • Object O2 = new Object();
    • O2 = O1; -> O1,O2 will share same object identity (O1 identity) and O2 will be unReachable Location (garbage) and will be deleted when garbage collector applied on Heap

    2.png

    Object O1;
    ///Zero Bytes have been Allocated in Heap
    O1 = new object();
    ///new (IL newObj)
    ///1.Allocate Required # of Bytes in Heap
    ///(Object Size + CLR Overhead Variables)
    ///2.Initialize(cross out ) allocted Bytes
    ///3.Call userdefined Ctor if exists
    ///4.Assign Refrence to newely allocated Object
    
    //object O2 = new object();
    object O2 = new(); ///C# 9.0 Syntax Sugar 
    
    Console.WriteLine(O1.GetHashCode());
    Console.WriteLine(O2.GetHashCode());
    
    O2 = O1;
    Console.WriteLine("After =");
    Console.WriteLine(O1.GetHashCode());
    Console.WriteLine(O2.GetHashCode());
    
    ///O2 , O1 Same Object , same identity  
    

  • String always differs from other data types →
    • int x; → allocation of 4 uninitialized bytes in stack.
    • string str; → it does not know how many bytes to allocate

  • String Formatting →

    int X = 17;
    
    float MyVar = 15.3f;
    double MyVar02 = 1456.321;
    decimal MyV = 15.3M;
    
    int X = 1_000_000; //_ : Discards
    X = 0x_00_00_FA_12; //Hex
    byte y = 0b_0101_1100; //Binary
    
    int X, Y;
    
    X = int.Parse(Console.ReadLine());
    Y = Convert.ToInt32(Console.ReadLine());
    
    ///C 
    ///printf ("%i + %i = %i" , X , Y , X+Y);
    
    //String Msg = string.Format("Equation : {0} + {1} = {2}", X, Y, X + Y);
    //Console.WriteLine(Msg);
    
    //Console.WriteLine("Equation : {0} + {1} = {2}", X, Y, X + Y);
    
    //string Msg = string.Format($"Equation : {X} + {Y} = {X + Y}");
    /////$ : String Manipulation Operator
    //Console.WriteLine(Msg);
    
    Console.WriteLine($"Equation : {X,5}+{Y,-5}={X + Y:X}");
    

  • If & Switch Ex01 →

    int X = 5;
    
    / Not Valid C# Code
    if (X) ;
    if (1) ;
    if (X = 3) ;
    
    if (X == 3)
        Console.WriteLine("Three"); ; ///Valid
    
    X = int.Parse(Console.ReadLine());
    if (X == 1)
        Console.WriteLine("Jan");
    else if (X == 2)
        Console.WriteLine("Feb");
    else if (X == 3)
        Console.WriteLine("Mar");
    else
        Console.WriteLine("Not in Q01");
    
    switch (X) // jump table becuz its num cases
    {
        case 1:
            Console.WriteLine("Jan");
            break;
        case 2:
            Console.WriteLine("Feb");
            break;
        case 3:
            Console.WriteLine("Mar");
            break;
        default:
            Console.WriteLine("Not in Q03");
            break;
    }
    
    String str = Console.ReadLine();
    
    switch (str)
    {
        case "a":
        case "A":
            Console.WriteLine("Option 01");
            break;
    
        case "B":
        case "b":
            Console.WriteLine("Option 02");
            break;
    
        case "c":
        case "C":
            Console.WriteLine("Option 03");
            break;
    }
    
    switch (str)
    {
        case "1":
            Console.WriteLine("Jan");
            break;
    
        case "2":
            Console.WriteLine("Feb");
            break;
        case "3":
            Console.WriteLine("Mar");
            break;
        case "4":
            Console.WriteLine("Apr");
            break;
        case "5":
            Console.WriteLine("May");
            break;
        case "6":
            Console.WriteLine("Jun");
            break;
        case "7":
            Console.WriteLine("Jul");
            break;
    
        default:
            Console.WriteLine("NA");
            break;
    }
    

  • Switch Ex02 →

    int Value = 3000;
    
    switch (Value)
    {
        case 3000:
            Console.WriteLine("Option 03");
            Console.WriteLine("Option 02");
            Console.WriteLine("Option 01");
            break;
        case 2000:
            Console.WriteLine("Option 02");
            Console.WriteLine("Option 01");
            break;
    
        case 1000:
            Console.WriteLine("Option 01");
            break;
        default:
            Console.WriteLine("NA");
            break;
    }
    
    switch (Value)
    {
        case 3000:
            Console.WriteLine("Option 03");
            //Console.WriteLine("Option 02");
            //Console.WriteLine("Option 01");
            goto case 2000;
        //break;
        case 2000:
            Console.WriteLine("Option 02");
            //Console.WriteLine("Option 01");
            goto case 1000;
        //break;
        case 1000:
            Console.WriteLine("Option 01");
            break;
        default:
            Console.WriteLine("NA");
            break;
    }
    

  • Block Statement →

    //object i = new object();
    
    //block statment
    {
        int i; /// block scope
    
        i = 7;
        i++;
        console.writeline(i);
    
    } ///end of i scope
    
    {
        string i;
    
        i = "abc";
    
        console.writeline(i.tolower());
    }
    

  • One D Array →

    / Array in C# , refrence Type
    / Object from Array class
    
    /C Style
    int Arr[5]
    
    int[] Arr;
    /Declaration for refrence of int Array 
    /Zero Bytes have been alllocated in Heap
    
    Arr = new int[5];
    
    int[] Arr = new int[5] { 3, 4, 5, 6, 7 };
    
    int[] Arr = new int[] { 3, 4, 5, 6, 7 };
    
    int[] Arr = { 3, 4, 5, 6, 7 };
    
    int[] Arr01 = { 1, 7, 5, 3, 8, 6, 4, 2 };
    
    Array.Sort(Arr01);
    
    Console.WriteLine($"Size {Arr01.Length} , Number of Dimensions {Arr01.Rank}");
    
    int[] Arr02 = { 7, 8, 9 };
    
    Console.WriteLine($"Arr01 {Arr01.GetHashCode()}");
    Console.WriteLine($"Arr02 {Arr02.GetHashCode()}");
    
    //Arr02 = Arr01;
    ///Same Object , Two References 
    Console.WriteLine("Arr02 = Arr01");
    
    Arr02 = (int[]) Arr01.Clone();
    ///int[] = (int[])object ;
    ///Derived = Base : Not Valid must use Explicit Casting
    /// ref To Base = Derived : Valid
    ///Arr02 new Object with new(diff) identity but same state as Arr01
    Console.WriteLine("After Clone");
    Console.WriteLine($"Arr01 {Arr01.GetHashCode()}");
    Console.WriteLine($"Arr02 {Arr02.GetHashCode()}");
    
    for (int i = 0; i<Arr02.Length; i++)
        Console.Write($"{Arr02[i]} , ");
    Console.WriteLine("");