tuple patterns
void Main()
{
RockPaperScissors ("paper", "rock").Dump();
}
public static string RockPaperScissors (string first, string second)
=> (first, second) switch
{
("rock", "paper") => "rock is covered by paper. Paper wins.",
("rock", "scissors") => "rock breaks scissors. Rock wins.",
("paper", "rock") => "paper covers rock. Paper wins.",
("paper", "scissors") => "paper is cut by scissors. Scissors wins.",
("scissors", "rock") => "scissors is broken by rock. Rock wins.",
("scissors", "paper") => "scissors cuts paper. Scissors wins.",
(_, _) => "tie"
};
positional patterns
void Main()
{
GetQuadrant (new Point (-1, 1)).Dump();
}
static Quadrant GetQuadrant (Point point) => point switch
{
(0, 0) => Quadrant.Origin,
var (x, y) when x > 0 && y > 0 => Quadrant.One,
var (x, y) when x < 0 && y > 0 => Quadrant.Two,
var (x, y) when x < 0 && y < 0 => Quadrant.Three,
var (x, y) when x > 0 && y < 0 => Quadrant.Four,
var (_, _) => Quadrant.OnBorder,
_ => Quadrant.Unknown
};
public class Point
{
public int X { get; }
public int Y { get; }
public Point (int x, int y) => (X, Y) = (x, y);
public void Deconstruct (out int x, out int y) =>
(x, y) = (X, Y);
}
public enum Quadrant
{
Unknown,
Origin,
One,
Two,
Three,
Four,
OnBorder
}
using declarations
void Main()
{
WriteLinesToFile ("the quick brown fox".Split());
}
static void WriteLinesToFile (IEnumerable<string> lines)
{
// Note the using statement without parenthesis or a block.
// It will dispose when the block (method in this case) exits.
using var file = new System.IO.StreamWriter ("WriteLines2.txt");
foreach (string line in lines)
{
// If the line doesn't contain the word 'Second', write the line to the file.
if (!line.Contains ("Second"))
{
file.WriteLine (line);
}
}
// file is disposed here
}
static local functions
void Main()
{
int y = 5;
int x = 7;
Add (x, y).Dump();
// The static keyword means we can't access local variables (x and y):
static int Add (int left, int right) => left + right;
}
disposable ref structs
void Main()
{
}
ref struct Foo // Cannot implement any interfaces
{
public int X, Y;
public void Dispose()
{
// Dispose...
}
}
async streams
async Task Main()
{
await foreach (var number in RangeAsync (0, 10))
Console.WriteLine (number);
// In LINQPad, you can also just dump the IAsyncEnumerable.
RangeAsync (0, 10).Dump();
}
public static async IAsyncEnumerable<int> RangeAsync (int start, int count)
{
for (int i = start; i < start + count; i++)
{
await Task.Delay (100);
yield return i;
}
}
indics and ranges
var words = new string[]
{
// index from start index from end
"The", // 0 ^9
"quick", // 1 ^8
"brown", // 2 ^7
"fox", // 3 ^6
"jumped", // 4 ^5
"over", // 5 ^4
"the", // 6 ^3
"lazy", // 7 ^2
"dog" // 8 ^1
}; // 9 (or words.Length) ^0
$"The last word is {words[^1]}".Dump();
words[1..4] .Dump ("words[1..4]");
words[^2..^0] .Dump ("words[^2..^0]");
words[..] .Dump ("words [..]");
words[..4] .Dump ("words[..4]");
words[6..] .Dump ("words[6..]");
Range phrase = 1..4;
var text = words[phrase].Dump ("range");
nullable reference types
#nullable enable
void Main()
{
}
class Person
{
public string FirstName; // Not null
public string? MiddleName; // May be null
public string LastName; // Not null
}
void M1 (Person p)
{
p.MiddleName.Length.Dump(); // WARNING: may be null
p.MiddleName!.Length.Dump(); // ok, you know best!
}
void M2 (Person p)
{
p.FirstName = null; // 1 WARNING: it's null
p.LastName = p.MiddleName; // 2 WARNING: may be null
string s = default (string); // 3 WARNING: it's null
string[] a = new string [10]; // 4 ok: too common
}
到此c#8 新增的内容都演示完了, 有问题的可以咨询我。
本文作者:wwmin
微信公众号: DotNet技术说
本文链接:https://www.jianshu.com/p/f12a4b31b601
关于博主:评论和私信会在第一时间回复。或者[直接私信]我。
版权声明:转载请注明出处!
声援博主:如果您觉得文章对您有帮助,关注点赞, 您的鼓励是博主的最大动力!
网友评论