Advertisement from Google

C# 6.0 has a lot of great new features, which save developer’s time and make code more clean and short. At the conclusion of the C# 6.0 series let’s go through the whole list of the new C# 6.0 features again (all titles are active). Each article contains detailed explanation of the particular feature with resulted IL code and “old-school” way of doing the same things.

var a = 1.2345; Console.WriteLine($"test {a}"); Console.WriteLine($"test {a} {a} {a}"); Console.WriteLine($"test {a + 10}"); Console.WriteLine($"test {a:00.00}"); Console.WriteLine($"test {a + 10:F2}"); Console.WriteLine($"test {Get10():F2}, {Get0()}"); Console.WriteLine($"test {(args.Length != 0 ? "0" : "1")}");

var user = repository.GetUser(); var street = user?.Address?.Street; var str = GetString(); return str?.Length ?? 0;

public void Method(int arg) { if (arg < 0) { throw new ArgumentOutOfRangeException(nameof(arg), $"In method: {nameof(Method)}"); } }

Advertisement from Google

public string Prop { get; } = "value"; public string Prop2 { get; set; } = "value";

try { Method(); } catch (Win32Exception ex) when (ex.NativeErrorCode == 0x07) { // do exception handling logic } catch (Win32Exception ex) when (ex.NativeErrorCode == 0x148) { // do exception handling logic } catch (Exception) { // log unhandled exception and rethrow throw; }

var dic = new Dictionary<string, int> { ["Apple"] = 2, ["Pear"] = 10 }; var list = new List<int> { [0] = 1, [1] = 2 };

public override string ToString() => "Name: " + name;

try { throw new Exception(); } catch (Exception) { await TestAsync(); } finally { await TestAsync(); }