索引

索引器 属于 C#

{other: "%{count} 个练习"}

关于 索引器

索引器允许通过索引运算符[]获取或设置对象的值。索引器接受一个实参,可以同时包含get和set部分,也可以只包含其中之一。索引器的set部分有一个特殊的value值,也就是传递给索引器的值。

class LapTimes
{
    private int[] times = new[] { 2, 4, 3, 8 };

    public int this[int lap]
    {
        get { return times[lap]; }
        set { times[lap] = value; }
    }
}

var lapTimes = new LapTimes();

// Use the getter
Console.WriteLine(lapTimes[1]); // => 4

// Use the setter
lapTimes[2] = 5;
Console.WriteLine(lapTimes[2]); // => 5

如果省略索引器的set部分,就会得到一个只读索引器。使用表达式主体可以让只读索引器的定义更简洁:

class LapTimes
{
    private int[] times = new[] { 2, 4, 3, 8 };

    public int this[int lap] => times[lap];
}

索引器的形参不必是int,可以是任何类型:

class LapTimes
{
    // ...
    public int this[string lap] => times[Convert.ToInt32(lap)];
}

索引器也可以重载:

class LapTimes
{
    public int this[int lap] => times[lap];
    public int this[string lap] => times[Convert.ToInt32(lap)];
}
通过 GitHub 编辑 该链接会在新窗口或标签页中打开