index和range的简单使用
1.
static void Main(string[] args)
{
string[] words = new string[]
{
"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
};
// Index
Index k = ^1;
Console.WriteLine(words[1]);
Console.WriteLine(words[words.Length - 1]);
Console.WriteLine(words[^1]);
Console.WriteLine(words[k]);
//range 范围
Range p = 0..3;
string[] lists = words[p];
foreach (var list in lists)
{
Console.WriteLine($"foreach:{list}");
}
Console.Read();
}
2.类中使用
class Program
{
static void Main(string[] args)
{
Point point = new Point();
Console.WriteLine(point[0]);
point[0] = "gongdonghai";
Console.WriteLine(point[0]);
Console.Read();
}
}
public class Point
{
public string[] gamma = new string[]
{
"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
};
//类级别索引:this
public string this[int index]
{
get
{
return gamma[index];
}
set
{
gamma[index] = value;
}
}
}