1. 索引和范围
以下 .net 类型同时支持索引和范围:array、string、span 和 readonlyspan 。 list 支持索引,但不支持范围
例一、获取身份证号码的生日
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); }
黄山市民网:https://www.huangshanshimin.com/