dotnet-easy / easy-dotnet

A Simple Way to Learn. NET

Home Page:https://easy-dotnet.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

F-park opened this issue · comments

commented

输出结果有误

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

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

其他的一些小建议

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