[WPF 學習] 5. 2 C#8.0的幾個有用的知識點

1. 索引和範圍
如下 .NET 類型同時支持索引和範圍:Array、String、Span 和 ReadOnlySpan 。 List 支持索引,但不支持範圍
例1、獲取身份證號碼的生日
函數

DateTime GetBirthdayFromIDNo(string idno)
        {
            if (idno.Length != 18)
                throw new Exception("身份證號碼不正確");
            return new DateTime(int.Parse(idno.Substring(6, 4)), int.Parse(idno.Substring(10, 2)), int.Parse(idno.Substring(12, 2)));
        }
        DateTime GetBirthdayFromIDNo2(string idno)
        {
            if (idno.Length != 18)
                throw new Exception("身份證號碼不正確");
            return new DateTime(int.Parse(idno[6..10]), int.Parse(idno[10..12]), int.Parse(idno[12..14]));
        }

例2、獲取字符串最後一位的內容code

var idNO = "330726197303273114";

            var s1 = idNO.Substring(idNO.Length - 1);
            var s2 = idNO.Last();
            var s3 = idNO[^1];

例3、移除最後最後一位的內容對象

var idNO = "330726197303273114";

            var s1 = idNO.Substring(0,idNO.Length - 1);
            var s2 = idNO.Remove(idNO.Length - 1);
            var s3 = idNO[..^1];

**2. switch
表達式索引

public enum Rainbow
{
    Red,
    Orange,
    Yellow,
    Green,
    Blue,
    Indigo,
    Violet
}

public static RGBColor FromRainbow(Rainbow colorBand) =>
    colorBand switch
    {
        Rainbow.Red    => new RGBColor(0xFF, 0x00, 0x00),
        Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
        Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
        Rainbow.Green  => new RGBColor(0x00, 0xFF, 0x00),
        Rainbow.Blue   => new RGBColor(0x00, 0x00, 0xFF),
        Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
        Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
        _              => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
    };

屬性模式ci

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

元組模式rem

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

位置模式
某些類型包含 Deconstruct 方法,該方法將其屬性解構爲離散變量。 若是能夠訪問 Deconstruct 方法,就能夠使用位置模式 檢查對象的屬性並將這些屬性用於模式。 考慮如下 Point 類,其中包含用於爲 X 和 Y 建立離散變量的 Deconstruct 方法:字符串

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

此外,請考慮如下表示象限的各類位置的枚舉:get

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

下面的方法使用位置模式 來提取 x 和 y 的值。 而後,它使用 when 子句來肯定該點的 Quadrant:string

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

3. Null 合併賦值it

List<int> numbers = null;
int? i = null;

numbers ??= new List<int>();
numbers.Add(i ??= 17);
numbers.Add(i ??= 20);

Console.WriteLine(string.Join(" ", numbers));  // output: 17 17
Console.WriteLine(i);  // output: 17

**4. 構造函數表達式

public class Test1
        {
            public int X { get; }
            public int Y { get; }
            public Test1(int x, int y)
            {
                X = x;
                Y = y;
            }
        }
        
        public class Test2
        {
            public int X { get; }
            public int Y { get; }
            public Test2(int x, int y) => (X, Y) = (x, y);
        }
相關文章
相關標籤/搜索