Value Type →
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 →
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 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("");