Last active
June 8, 2020 20:11
-
-
Save ufcpp/3b0f78de250dc94243bd8015250779b0 to your computer and use it in GitHub Desktop.
// new features in .NET 5 preview 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// new features in .NET 5 preview 4 | |
using System; | |
using System.Collections.Generic; | |
interface IA { int A(); } | |
interface IB : IA { int B(); } | |
interface IC : IA { int C(); } | |
class Ab : IB, IC | |
{ | |
public int A() => 2; | |
public int B() => 3; | |
public int C() => 5; | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
// native int | |
nint i = 1; | |
nint u = 2; | |
Console.WriteLine(i + u); | |
} | |
// target-typed new | |
static Dictionary<string, int> d = new() | |
{ | |
{ "a", 1 }, | |
}; | |
// pattern V3 (and, or, not, simple type, parenthesized...) | |
static int M(object obj) => obj switch | |
{ | |
IB b and IC c => b.A() * b.B() * c.C(), | |
(IA or IB) and var a => a.A(), | |
DateTime => 1, | |
not null => 0, | |
null => -1, | |
}; | |
// pattern V3 (rerational, exhaustivity check) | |
static int M(int value) => value switch | |
{ | |
< 100 => 1, | |
>= 100 and <= 500 => 2, | |
> 500 => 3, | |
}; | |
// pattern V3 (byte exhaustivity check) | |
static int M(byte value) => value switch | |
{ | |
<= 31 => 0, | |
32 => 2, | |
<= 47 => 3, | |
<= 57 => 4, | |
<= 64 => 3, | |
<=90 => 5, | |
<= 96 => 3, | |
<= 122 => 5, | |
<= 126 => 3, | |
127 => 0, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment