索引器有什么好处?

use*_*430 3 c# indexer

有人可以解释一个索引器有什么好处?

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)

Mar*_*ell 7

它纯粹是语法上的方便性和可读性/表现力.它仍然作为一种方法实现.所以:如果你认为target[0]你的场景更加明显,方便和可读:使用索引器.