1. 程式人生 > 其它 >Java網路程式設計04:UDP連線

Java網路程式設計04:UDP連線

 

https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-8#property-patterns  C# 8.0 

非同步流 : 生成非同步流

public static async System.Collections.Generic.IAsyncEnumerable<int> GenerateSequence()
{
    for (int i = 0; i < 20; i++)
    {
        await Task.Delay(100);
        yield return i;
    }
}
View Code

使用非同步流 : await foreach 

await foreach (var number in GenerateSequence())
{
    Console.WriteLine(number);
}
View Code

非同步可釋放

await using 

 

屬性模式

public static decimal ComputeSalesTax(Address location, decimal salePrice) =>
    location switch
    {
        { State: "WA" } => salePrice * 0.06M
, { State: "MN" } => salePrice * 0.075M, { State: "MI" } => salePrice * 0.05M, // other cases removed for brevity... _ => 0M };
View Code

元組模式:

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" };
View Code

位置模式:

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);
}
View Code

 

public enum Quadrant
{
    Unknown,
    Origin,
    One,
    Two,
    Three,
    Four,
    OnBorder
}

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
};
View Code

 

教程:使用模式匹配來生成型別驅動和資料驅動的演算法 

https://docs.microsoft.com/zh-cn/dotnet/csharp/fundamentals/tutorials/pattern-matching  

 

 

 

 

 

 

 

 

 

 

 

 

END