1. 索引和範圍 以下 .NET 類型同時支持索引和範圍:Array、String、Span 和 ReadOnlySpan。 List 支持索引,但不支持範圍 例一、獲取身份證號碼的生日 例二、獲取字元串最後一位的內容 例三、移除最後最後一位的內容 2. switch 表達式 屬性模式 元組模式 位 ...
1. 索引和範圍
以下 .NET 類型同時支持索引和範圍:Array、String、Span
例一、獲取身份證號碼的生日
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]));
}
例二、獲取字元串最後一位的內容
var idNO = "330726197303273114";
var s1 = idNO.Substring(idNO.Length - 1);
var s2 = idNO.Last();
var s3 = idNO[^1];
例三、移除最後最後一位的內容
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)),
};
屬性模式
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
};
元組模式
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);
}
此外,請考慮以下表示象限的各種位置的枚舉:
public enum Quadrant
{
Unknown,
Origin,
One,
Two,
Three,
Four,
OnBorder
}
下麵的方法使用位置模式 來提取 x 和 y 的值。 然後,它使用 when 子句來確定該點的 Quadrant:
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 合併賦值
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);
}