easy-dotnet icon indicating copy to clipboard operation
easy-dotnet copied to clipboard

【范围运算符】输出结果有误 & 内容重复

Open F-park opened this issue 1 year ago • 0 comments

输出结果有误

在小标题选取从结尾位置开始的一段范围中,文档代码如下

int[] numbers = new[] { 0, 10, 20, 30, 40, 50 };
int amountToTakeFromEnd = 3;
int[] subset = numbers[^amountToTakeFromEnd..];
Display(subset);  // output: 20 30 40

output 的结果应为 30 40 50

内容重复

我发现选取从结尾位置开始的一段范围省略右操作数基本一致,不知道是否为有意为之。文档代码如下

int[] numbers = new[] { 0, 10, 20, 30, 40, 50 };
int[] subset = numbers[^3..];
Display(subset);  // output: 30 40 50

其他的一些小建议

选取数组中的范围中的四组代码通过只使用一次的变量来使用范围运算符,感觉降低了可读性

F-park avatar Oct 15 '23 09:10 F-park