1代表开始取值的索引,3代表最后一个取值的后一个索引。
示例
Program.cs
//create an array
string[] myFriends = new string[] { "Scott", "Allen", "Jones", "James", "Sara" };
//traditional way
Console.WriteLine(myFriends[myFriends.Length - 1]);
//"index from end" operator (^)
Console.WriteLine(myFriends[^1]); //Sara
Console.WriteLine(myFriends[^5]); //Scott
//Index struct
Index i = ^4;
Console.WriteLine(myFriends[i]); //Scott
//Range - LINQ
//IEnumerable<string> shortListedFriends = myFriends.Skip(1).Take(3);
Range r = 1..4;
IEnumerable<string> shortListedFriends = myFriends[r];
Console.WriteLine("Short listed friends:");
foreach (string friend in shortListedFriends)
{
Console.Write(friend + ", ");
}
Console.WriteLine();
Console.ReadKey();