比如我有如下collections
string[] names = new[] {"tom0", "tom1", "tom2", "tom3", "tom4", "tom5", "tom6"};
int[] items = new[] {0,1,2,3,4,5,6};
我要取倒数第二个items的值(5),.net 6可以用^符号表示反转,区别如下:
items.Reverse().ElementAt(1).Dump(); // old style
items.ElementAt(^2).Dump(); // .net 6
我要取items第2到第5区间的sub collections,可以用 x..y 语法
items.Skip(2).Take(3).Dump(); // old style
items.Take(2..5).Dump(); // .net 6
把items和names结合起来,生成一个新的Tuple<int, string> collection
items.Zip(names).Dump(); // .net 6
$ irb 2.6.6 :001 > names = ["tom0", "tom1", "tom2", "tom3", "tom6"] => ["tom0", "tom1", "tom2", "tom3", "tom6"] 2.6.6 :002 > names[-2] => "tom3" 2.6.6 :003 > names[2..5] => ["tom2", "tom3", "tom6"] 2.6.6 :004 > items = [0,1,2,3,4,5,6] => [0, 1, 2, 3, 4, 5, 6] 2.6.6 :005 > items.zip(names) => 0, "tom0"], [1, "tom1"], [2, "tom2"], [3, "tom3"], [4, "tom6"], [5, Nil], [6, Nil 2.6.6 :006 >