C# 范围运算符
a..
等效于a..^0
..b
等效于0..b
..
等效于0..^0
-
范围运算符表达式 说明 ..
集合中的所有值。 ..end
从开头到 end
(不含)的值。start..
从 start
(含)到结尾的值。start..end
从 start
(含)到end
(不含)的值。^start..
从 start
(含)到倒计数结尾的值。..^end
从开头到 end
(不含,倒计数)的值。start..^end
从 start
(含)到end
(不含,倒计数)的值。^start..^end
从 start
(含)到end
(不含),均为倒计数。以下示例演示了使用上表中所有范围的效果:
int[] oneThroughTen = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; Write(oneThroughTen, ..); Write(oneThroughTen, ..3); Write(oneThroughTen, 2..); Write(oneThroughTen, 3..5); Write(oneThroughTen, ^2..); Write(oneThroughTen, ..^3); Write(oneThroughTen, 3..^4); Write(oneThroughTen, ^4..^2); static void Write(int[] values, Range range) => Console.WriteLine($"{range}:\t{string.Join(", ", values[range])}"); // Sample output: // 0..^0: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // 0..3: 1, 2, 3 // 2..^0: 3, 4, 5, 6, 7, 8, 9, 10 // 3..5: 4, 5 // ^2..^0: 9, 10 // 0..^3: 1, 2, 3, 4, 5, 6, 7 // 3..^4: 4, 5, 6 // ^4..^2: 7, 8
本文来自博客园,作者:willamyao,转载请注明原文链接:https://www.cnblogs.com/robertyao/p/17934287.html
代码改变世界