1. 索引和范围 以下 .NET 类型同时支持索引和范围:Array、String、Span<T> 和 ReadOnlySpan<T>。 List<T> 支持索引,但不支持范围 例一、获取身份证号码的生日
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);
}