C# 6.0C# 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.

String Interpolation

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")}");

Null Propagation

var user = repository.GetUser();
var street = user?.Address?.Street;

var str = GetString();
return str?.Length ?? 0;

nameof Operator

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

Auto-Property Initializers

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

Exception Filters

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;
}

Index Initializer

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

var list = new List<int>
{
    [0] = 1,
    [1] = 2
};

Expression-Bodied Methods

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

await in catch/finally

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