Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created June 9, 2017 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ufcpp/6cd44a847db870559a3fccb4c64d4cb2 to your computer and use it in GitHub Desktop.
Save ufcpp/6cd44a847db870559a3fccb4c64d4cb2 to your computer and use it in GitHub Desktop.
C# 7.1 (VS 2017 U3 Preview 2 時点。リリースでも、おそらくこれで全部になりそう。)
using System;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
// Main の戻り値を Task にしても、エントリーポイントとして認識されるようになった。
static async Task Main()
{
for (int i = 10; i > 0; i--)
{
Console.WriteLine(i);
await Task.Delay(1000);
}
Console.WriteLine("Hello World!");
}
// タプル構築時、var t = (x, y) と書くだけで、t.x (x っていう名前が付く)が使えるようになった。
static void TupleProjection()
{
var x = 1;
var y = 2;
var t = (x, y);
Console.WriteLine(t.x);
}
// default(T) の (T) が、左辺から推論できる場合には不要になった。
// default 単体で式扱い。
static void DefaultExpression(CancellationToken cancellationToken = default)
{
while (cancellationToken != default && !cancellationToken.IsCancellationRequested)
{
Thread.Sleep(1000);
Console.WriteLine(".");
}
}
// オープン型(型引数になっていて、具体的な型が確定してないもの)に対してパターン マッチングを使えるようになった。
static void PatternWithGenerics<T>(T x)
{
switch (x)
{
case int i:
break;
case string s:
break;
}
}
class Base { }
class Derived1 : Base { }
class Derived2 : Base { }
class Derived3 : Base { }
// こういう、型制約付きのやつですら 7.0 ではダメだった<オープン型に対してパターン マッチング。
static void PatternWithGenerics2<T>(T x)
where T : Base
{
switch (x)
{
case Derived1 d:
break;
case Derived2 d:
break;
case Derived3 d:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment