class OutEnhancements { public static void Main() { Fun(out int v); // declare variable v and pass it to Fun() as out parameter Console.WriteLine(v); // Variable v can be used in enclosing block. Fun(out var v2); Console.WriteLine(v2); } public static void Fun(out int value) { value = 100; } }
class Tuples { public static void Main() { var info = GetInfo(); Console.WriteLine($"{info.Item1} , { info.Item2}"); var details = GetDetails(); Console.WriteLine($"{details.mobile}, {details.email}"); // Accessing members using names provided by function } // Return a Tuple of two values public static (string,string) GetInfo() { return ("9059057000", "srikanthpragada@gmail.com"); } // Return values with names public static (string mobile, string email) GetDetails() { return ("9059057000", "srikanthpragada@gmail.com"); } }
class MyTime { public int Hours { get; set; } = DateTime.Now.Hour; public int Mins { get; set; } = DateTime.Now.Minute; public int Secs { get; set; } = DateTime.Now.Second; public (int , int , int ) GetTime() { return (Hours, Mins, Secs); } public static MyTime Now { get => new MyTime(); } // Deconstruct method to copy data to out parameters public void Deconstruct(out int h, out int m, out int s) { h = Hours; m = Mins; s = Secs; } } class Deconstruction { public static void Main() { MyTime t1 = new MyTime(); // Deconstruction of Tuple into three variables var (hour, min, sec) = t1.GetTime(); Console.WriteLine($"{hour}:{min}:{sec}"); // Deconstruction of MyTime into variable declared using var individually (var hr, var mi, var se) = MyTime.Now; var (h, m, s) = MyTime.Now; // Another example for deconstruction Console.WriteLine($"{h}:{m}:{s}"); } }
class LocalFunctions { public static void Main() { Console.WriteLine(NextEven(10)); // Local Function - can be used only in Main() int NextEven(int v) { return v % 2 == 0 ? v + 2 : v + 1; } } }
int num = 10_4_232; int binary = 0b1111; Console.WriteLine(num); Console.WriteLine(binary);
class Stack { private int top; public Stack() => top = -1; // Expression body in constructor public int Length { get => top + 1; // Expression body in getter method of property } }
class RefReturns { public static void Main() { int[] a = { 10, 20, 30, 40, 50 }; ref int refto20 = ref Find(a, 1); // get a reference to element at index 1 with value 20 Console.WriteLine(refto20); refto20 = 25; // modify value at index 1 pointed by reference Console.WriteLine(a[1]); } // Function returns a Reference and not Value public static ref int Find(int [] values, int pos) { return ref values[pos]; // return a reference } }
class ThrowExceptionFromExpression { public static void Main() { int v = 10; Process(v); } public static int Process(int v) { // Throw an Exception from an expression return v > 10 ? v + 1 : throw new ArgumentException("Invalid Number!"); } }
object v = 10; Console.WriteLine(v is int); // True if v contains int // Take value from v into i only when v is of type int if (v is int i) Console.WriteLine(i); else Console.WriteLine("Not an int");
object v = "Srikanth"; switch(v) { case String s when (s.Length > 5): // Extract value and test its value with condition Console.WriteLine($"Large String {s}"); break; case String s: // if it is a string then extract value into s Console.WriteLine($"String {s}"); break; case DateTime d: Console.WriteLine($"Date {d}"); break; case int n: Console.WriteLine($"Integer {n}"); break; }