3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

1分で分かる C# 7.1 の新機能

Posted at

What's new in C# 7.1Welcome to C# 7.1 をベースに、C# 7.0 と比較してコードがどう変わるのかを記載しています。

1. async Main メソッド (async Main method)

old
static void Main() {
    MainAsync().GetAwaiter().GetResult();
}

static async Task MainAsync() {
    ... // await code
}
:arrow_down:
```csharp:new! static async Task Main() { ... // await code } ``` ※ `Main` メソッドに `async` を修飾できるのは、戻り値の型が `Task` or `Task` の場合のみです。`async void Main` はコンパイルエラー CS5001 となります。[詳細はこちら](https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.1/async-main.md#detailed-design)。

2. default リテラル式 (default literal expressions)

old
Func<string, bool> whereClause = default(Func<string, bool>);
:arrow_down:
```csharp:new! Func whereClause = default; ```

3. 推論されたタプル要素の名前 (Inferred tuple element names)

別名、タプル プロジェクション初期化子 (Tuple projection initializers)

old
int width, height;
...
var rect = (width:width, height:height);
Console.WriteLine($"{rect.width}x{rect.height}");
:arrow_down:
```csharp:new! int width, height; ... var rect = (width, height); Console.WriteLine($"{rect.width}x{rect.height}"); ```

4. pattern-matching with generics

C# 7.0 パターンマッチングの bug-fix レベルの変更。
以前はオープン型の式(変数)のマッチングが行えませんでしたが、C# 7.1 で対応されました。

old
public interface IShape {}
public struct Rectangle : IShape {}

public void Draw<T>(T shape) where T: IShape {
    // キャストが必要。外すとコンパイルエラー。
    if ((IShape)shape is Rectangle rect1) {
        ...
    }

    // キャストが必要。外すとコンパイルエラー。
    switch ((IShape)shape) {
        case Rectangle rect2:
            ...
            break;
    }
}
:arrow_down:
```csharp:new! public interface IShape {} public struct Rectangle : IShape {}

public void Draw(T shape) where T: IShape {
if (shape is Rectangle rect1) {
...
}

switch (shape) {
    case Rectangle rect2:
        ...
        break;
}

}


## For more information
- [What's new in C# 7.1 | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/csharp/whats-new/csharp-7-1)
- [Welcome to C# 7.1 | .NET Blog](https://blogs.msdn.microsoft.com/dotnet/2017/10/31/welcome-to-c-7-1/)
- [csharplang/Language-Version-History.md at master · dotnet/csharplang > C# 7.1 (Visual Studio 2017 version 15.3)](https://github.com/dotnet/csharplang/blob/master/Language-Version-History.md#c-71-visual-studio-2017-version-153)


## Related Posts
- [10分で分かる C# 7.0 の新機能](https://qiita.com/inasync/items/bba3e4b9d370b7e5589e)
- [3分で分かる C# 6 の新機能](https://qiita.com/inasync/items/c653d7a952a873fef8b0)
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?