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 };
using 声明
using 声明是前面带 using 关键字的变量声明。它指示编译器声明的变量应在封闭范围的末尾进行处理。
staticintWriteLinesToFile(IEnumerable<string> lines) { // 当到达方法的右括号时,将对该文件进行处理。 usingvar file = new System.IO.StreamWriter("WriteLines2.txt"); // Notice how we declare skippedLines after the using statement. int skippedLines = 0; foreach (string line in lines) { if (!line.Contains("Second")) { file.WriteLine(line); } else { skippedLines++; } } // Notice how skippedLines is in scope here. return skippedLines; // file is disposed here }
静态本地函数
1 2 3 4 5 6 7 8
intM() { int y = 5; int x = 7; return Add(x, y);
staticintAdd(int left, int right) => left + right; }
var words = newstring[] { // index from start index from end "The", // 0 ^9 "quick", // 1 ^8 "brown", // 2 ^7 "fox", // 3 ^6 "jumped", // 4 ^5 "over", // 5 ^4 "the", // 6 ^3 "lazy", // 7 ^2 "dog"// 8 ^1 }; // 9 (or words.Length) ^0
// 使用 ^1 索引检索最后一个词 Console.WriteLine($"The last word is {words[^1]}"); // writes "dog"
// 包括 words[1] 到 words[3] var quickBrownFox = words[1..4];
// 包括 words[^2] 和 words[^1] var lazyDog = words[^2..^0];
var allWords = words[..]; // contains "The" through "dog". var firstPhrase = words[..4]; // contains "The" through "fox" var lastPhrase = words[6..]; // contains "the", "lazy" and "dog"