有人可以解释一个索引器有什么好处?
public class MyClass
{
private List<string> list = new List<string>()
public string this[int value]
{
get
{
return list[value];
}
}
public string GetValue(int value)
{
return list[value];
}
}
Run Code Online (Sandbox Code Playgroud)
使用的好处是什么:
MyClass target = new MyClass();
string value = target[0];
Run Code Online (Sandbox Code Playgroud)
对此:
MyClass target = new MyClass();
string value = target.GetValue(0);
Run Code Online (Sandbox Code Playgroud)