using static System.Console; // Import all static members of Console class using static System.DateTime; // Import all static members of DateTime class namespace Csharp6_features { class StaticUsing { public static void Main() { WriteLine("Hello!"); // we don't have to use Console.WriteLine anymore WriteLine(Today); } } }
using static System.Console; using static System.DateTime; namespace Csharp6_features { class AutoInitProperty { public static void Main() { MyTime t = new MyTime(); WriteLine("{0}:{1}:{2}", t.Hours, t.Mins, t.Secs); } } class MyTime { public int Hours { get; set; } = Now.Hour; // set Hours property to value of given expression public int Mins { get; set; } = Now.Minute; public int Secs { get; set; } = 0; // set Secs property to 0 } }
using System; namespace Csharp6_features { class StringInterpolation { public static void Main() { string name = "Anders"; int age = 55; // Before C# 6.0 Console.WriteLine("Name : {0}, Age : {1:D2}", name, age); // In C# 6.0 Console.WriteLine($"Name : {name}, Age : {age:D2}"); } } }
using System; using System.Collections.Generic; namespace Csharp6_features { class DictionaryInit { public static void Main() { // Before C# 6.0 var products = new Dictionary<string, double>() { { "iPhone", 60000 }, { "iMac", 90000 }, { "iPad", 45000 }, }; foreach (var prod in products) { Console.WriteLine($"{prod.Key} - {prod.Value}"); } // In C# 6.0 var products2 = new Dictionary<string, double>() { ["iPhone"] = 60000, ["iMac"] = 95000, ["iPad"] = 45000 }; foreach (var prod in products2) { Console.WriteLine($"{prod.Key} - {prod.Value}"); } } } }
using System; namespace Csharp6_features { class ExpressionBodiedFunction { public static int Add(int n1, int n2) { return n1 + n2; // old way of doing it } public static int Mul(int n1, int n2) => n1 * n2; // Possible in C# 6.0 public static void Main() { Console.WriteLine(Add(10, 20)); Console.WriteLine(Mul(10, 20)); } } }
catch(ExceptionClass ex) when (condition) { }
using System; namespace Csharp6_features { class ExceptionFilter { public static void Main() { bool debug = true; try { int n1 = 10, n2 = 0; Console.WriteLine(n1 / n2); } catch (Exception ex) when (ex.InnerException != null) { Console.WriteLine(ex.InnerException.Message); } catch (Exception ex) when (debug) { Console.WriteLine("Debugging : " + ex.Message); } catch (Exception ex) { Console.WriteLine("Error : " + ex.Message); } } } }
using System; namespace Csharp6_features { class NameOfDemo { public static void Main() { int amount = 10; // variable name is hard-coded, when amount variable is renamed, message remains same if (amount < 10) throw new ArgumentException("Value of amount is invalid"); // When amount variable is renamed, message is automatically updated if (amount > 100) throw new ArgumentException("Value of " + nameof(amount) + " is too high!"); // Same as above, but uses String interpolation if (amount > 50) throw new ArgumentException($"Value of {nameof(amount)} is high!"); } } }
using System; using System.Collections.Generic; namespace Csharp6_features { class NullConditional { public static void Main() { String s = null; // Before C# 6.0, we have to check whether s points to null before using it otherwise it throws exception if (s == null) Console.WriteLine("null"); else Console.WriteLine(s.ToUpper()); // C# 6.0 checks whether s is null, if it is null it uses expression given after ?? otherwise it proceeds Console.WriteLine(s?.ToUpper() ?? "Null"); } } }