4-索引中的那些操作

哈喽,放起假的第一天。

接着之前的文档来观看,看到了索引。

 

索引这个东西我们都很熟悉,就不太详细介绍了。

但是今天看文档的时候发现还有一些新操作,先上代码看一眼。

 1             string[] words = new string[] 
 2             {
 3                     // index from start index from end 
 4             "The",  // 0 ^9 "quick", // 1 ^8 
 5             "brown", // 2 ^7 "fox", // 3 ^6 
 6             "jumped", // 4 ^5 
 7             "over", // 5 ^4 
 8             "the", // 6 ^3 
 9             "lazy", // 7 ^2 
10             "dog" // 8 ^1 
11             }; // 9 (or words.Length) ^0
12 
13             Console.WriteLine($"The last word is {words[^0]}");

 

前面的都是很基础的操作,但是第13行在通过索引取值的时候,发现了  【^0】 这么个玩意儿,

之前取值一般都是通过下标获取,或者在循环中依次迭代去获取。 但,words【^0】 这是个什么玩意儿?

 

 

 

 

 

这下就明白了,但不幸的是,在我的vs15中这种写法跑不起来。

没办法去跑出来看一下了。

有兴趣有vs19的童鞋可以去试试看贴出来。

还有这种写法

string[] quickBrownFox = words[1..4]

这种写法

string[] lazyDog = words[^2..^0];

当然我们主要使用的还是变量方式取值。

int x = 12;
int y = 25;

Console.WriteLine($"{numbers[x..y].Length} is the same as {y - x}"); 
Console.WriteLine("numbers[x..y] and numbers[y..z] are consecutive and disjoint:");

 

好了,索引就说到这里,除了新版本带来的惊喜外,其他也没什么好说的。

posted @ 2020-10-09 11:40  王月半子  阅读(120)  评论(0编辑  收藏  举报