interface ICounter { void Inc(); void Dec(); // Default method string Author() { return "Srikanth Pragada"; } } class MyCounter : ICounter { private int value = 1; public void Dec() { this.value--; } public void Inc() { this.value += 1; } } class InterfaceMethods { static void Main(string[] args) { ICounter c = new MyCounter(); c.Inc(); Console.WriteLine(c.Author()); } }
int disrate = code switch { 1 => 10, 2 => 20, 3 => 25, _ => 5 // Rest of the values - similar to default };
// Find out discount rate based on code and qty int disrate = (code,qty) switch { (1,1) => 10, (1,2) => 20, (2,1) => 15, (2,2) => 30, (_,_) => 5 };
int days = month switch { 2 => 28, 4 or 6 or 9 or 11 => 30, _ => 31 };
static void Main(string[] args) { using var f = new StreamReader("names.txt"); // use f to read data // f is automatically disposed at the end of block }
int? v = null; v ??= 10; // assigns 10 as v is null v ??= 20; // doesn't change value of v as it already has value Console.WriteLine(v); // prints 10 String name = null; Console.WriteLine(name ?? "Unknown" ); // prints Unknown name = "C#"; Console.WriteLine(name ?? "Unknown"); // prints C#
var names = new string[] { "C#", "Java", "JavaScript", "Python" }; Console.WriteLine(names[^1]); // Python Console.WriteLine(names[^4]); // C# // First two entries foreach (var s in names[0..2]) // C# and Java Console.WriteLine(s); // Last two entries foreach (var s in names[^2..]) // JavaScript and Python Console.WriteLine(s); // Strings also support ^ and .. Console.WriteLine( "Hello"[^1]); // o Console.WriteLine("Hello"[0..4]); // Hell