pro*_*mer 5 c# syntax array-indexing
int []arr = new int[4];
arr[^1]; // returns the last element
Run Code Online (Sandbox Code Playgroud)
我正在尝试弄清楚上面的语法。它返回最后一个元素,但为什么呢?
C# 8.0 及以后声明了新的范围和索引
其中^运营商:
让我们从指数规则开始。考虑一个数组序列。索引
0与 相同sequence[0]。索引^0与 相同sequence[sequence.Length]。
所以它是一种反向搜索可索引对象的方法,而不需要像sequence[sequence.Length - i].
string[] words = new string[]
{
// 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1333 次 |
| 最近记录: |