3分で分かる C# 6 の新機能

[What's New in C# 6](https://docs.microsoft.com/ja-jp/dotnet/csharp/whats-new/csharp-6)、[New Language Features in C# 6](https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6) をベースに、C# 5 と比較してコードがどう変わるのかを、主な機能に絞って記載しています。

## 1. [自動プロパティの機能強化 (Auto-Property enhancements)](https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties)
### 自動プロパティの初期化子 (Auto-Property initializers)
```csharp:old
public class Customer {
    public Customer() {
        First = "Jane";
        Last = "Doe";
    }

    public string First { get; set; }
    public string Last { get; set; }
}
```
<div>:arrow_down:</div>
```csharp:new!
public class Customer {
    public string First { get; set; } = "Jane";
    public string Last { get; set; } = "Doe";
}
```
### 読み取り専用の自動プロパティ (Read-only auto-properties)
```csharp:old
public class Customer {
    private readonly string _name;

    public Customer(string name) {
        _name = name;
    }

    public string Name { get { return _name; } }
}
```
<div>:arrow_down:</div>
```csharp:new!
public class Customer {
    public Customer(string name) {
        Name = name;
    }

    public string Name { get; }
}
```


## 2. [式形式のメンバー (Expression-bodied function members)](https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members)
### 式形式のメソッド (Expression bodies on method-like members)
```csharp:old
public void Print() {
    Console.WriteLine(First + " " + Last);
}
```
<div>:arrow_down:</div>
```csharp:new!
public void Print() => Console.WriteLine(First + " " + Last);
```
### 式形式のプロパティ (Expression bodies on property-like function members)
```csharp:old
public string Name {
    get {
        return First + " " + Last;
    }
}
```
<div>:arrow_down:</div>
```csharp:new!
public string Name => First + " " + Last;
```


## 3. [using static](https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/keywords/using-static)
```csharp:old
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.Sqrt(3*3 + 4*4)); 
        Console.WriteLine(DayOfWeek.Friday - DayOfWeek.Monday); 
    }
}
```
<div>:arrow_down:</div>
```csharp:new!
using static System.Console;
using static System.Math;
using static System.DayOfWeek;

class Program {
    static void Main() {
        WriteLine(Sqrt(3*3 + 4*4)); 
        WriteLine(Friday - Monday); 
    }
}
```


## 4. [Null 条件演算子 (Null-conditional operators)](https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/operators/null-conditional-operators)
```csharp:old
var handler = PropertyChanged;
if (handler != null) {
    handler(this, args);
}
```
<div>:arrow_down:</div>
```csharp:new!
PropertyChanged?.Invoke(this, args);
```


## 5. [文字列補間 (String interpolation)](https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/keywords/interpolated-strings)
```csharp:old
var s = String.Format("{0,20} is {1:D3} year{2} old", p.Name, p.Age, (p.Age == 1 ? "" : "s"));
```
<div>:arrow_down:</div>
```csharp:new!
var s = $"{p.Name,20} is {p.Age:D3} year{(p.Age == 1 ? "" : "s")} old";
```


## 6. [`nameof` 式 (`nameof` Expressions)](https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/keywords/nameof)
```csharp:old
if (x == null) throw new ArgumentNullException("x");
```
<div>:arrow_down:</div>
```csharp:new!
if (x == null) throw new ArgumentNullException(nameof(x));
```


## 7. [インデックス初期化子 (Index initializers)](https://docs.microsoft.com/ja-jp/dotnet/csharp/whats-new/csharp-6#index-initializers)
```csharp:old
var numbers = new Dictionary<int, string> {
    {7, "seven"},
    {9, "nine"},
    {13, "thirteen"},
};
```
<div>:arrow_down:</div>
```csharp:new!
var numbers = new Dictionary<int, string> {
    [7] = "seven",
    [9] = "nine",
    [13] = "thirteen",
};
```


## 8. [例外フィルター (Exception filters)](https://docs.microsoft.com/ja-jp/dotnet/csharp/whats-new/csharp-6#exception-filters)
```csharp:old
try { … }
catch (MyException e) {
    if (myfilter(e)) {
        …
    }
}
```
<div>:arrow_down:</div>
```csharp:new!
try { … }
catch (MyException e) when (myfilter(e)) {
    …
}
```


## 9. [catch / finally ブロックでの await (Await in catch and finally blocks)](https://docs.microsoft.com/ja-jp/dotnet/csharp/whats-new/csharp-6#await-in-catch-and-finally-blocks)
```csharp:old
Resource res = null;
try {
    res = await Resource.OpenAsync(…);
    …
} 
catch(ResourceException e) {
    Resource.LogAsync(res, e).Wait();
}
finally {
    if (res != null) {
        res.CloseAsync().Wait();
    }
}
```
<div>:arrow_down:</div>
```csharp:new!
Resource res = null;
try {
    res = await Resource.OpenAsync(…);
    …
} 
catch(ResourceException e) {
    await Resource.LogAsync(res, e);
}
finally {
    if (res != null) {
        await res.CloseAsync();
    }
}
```


## For more information
- [New Language Features in C# 6 · dotnet/roslyn Wiki](https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6)
- [What's New in C# 6 - C# Guide | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/csharp/whats-new/csharp-6)
- [C# 6 の新機能 - C# によるプログラミング入門 | ++C++; // 未確認飛行 C](http://ufcpp.net/study/csharp/ap_ver6.html)