C# what is the point or benefit of an indexer?

后端 未结 11 2033
暖寄归人
暖寄归人 2020-12-16 16:07

Doing some code reading and stumbled upon this snippet that I haven\'t seen before:

public SomeClass {
  public someInterface this[String strParameter] {
            


        
相关标签:
11条回答
  • 2020-12-16 16:16

    See the language specification, section 10.9, which states:

    An Indexer is a member that enables an object to be indexed in the same way as an array.

    Indexers and properties are very similar in concept, but differ in the following ways:

    • A property is identified by its name, whereas an indexer is identified by its signature.
    • A property is accessed through a simple-name (§7.5.2) or a member-access (§7.5.4), whereas an indexer element is accessed through an element-access (§7.5.6.2).
    • A property can be a static member, whereas an indexer is always an instance member.
    • A get accessor of a property corresponds to a method with no parameters, whereas a get accessor of an indexer corresponds to a method with the same formal parameter list as the indexer.
    • A set accessor of a property corresponds to a method with a single parameter named value, whereas a set accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus an additional parameter named value.
    • It is a compile-time error for an indexer accessor to declare a local variable with the same name as an indexer parameter.
    • In an overriding property declaration, the inherited property is accessed using the syntax base.P, where P is the property name. In an overriding indexer declaration, the inherited indexer is accessed using the syntax base[E], where E is a comma separated list of expressions.
    0 讨论(0)
  • 2020-12-16 16:17

    That's called an Indexer, they allow you to use List<>, ArrayList, Dictionary<> and all the other collections using an array syntax.

    It's just syntactic sugar, but it gives some readability when used right.

    0 讨论(0)
  • 2020-12-16 16:19

    You may have already stumbled across something similar before:

    var myList = new List<string>();
    myList.Add("One");
    myList.Add("Two");
    myList.Add("Three");
    
    for(int i = 0; i < myList.Count; i++) {
        string s = myList[i];
    }
    

    The indexer is the "primary key" of an object that implements a collection. It's just a shorthand way for writing a function like .GetValue(index) - syntactic sugar, if you want. But it also makes the intent clear.

    0 讨论(0)
  • 2020-12-16 16:21

    In many cases, the 'index' syntax makes a lot of sense. It is particularly useful if the SomeClass represents some sort of collection.

    0 讨论(0)
  • 2020-12-16 16:25

    It an implementation of the index operator [ ].

    0 讨论(0)
  • 2020-12-16 16:25

    In my opinion, it's just a syntax convenience. Where would you NOT use it:

    public SomeClass {
      private int[] nums;
      public GetVal this[int ind] {
        get {
          return nums[ind]; // this is pointless since array is already indexed
        }
      }
    }
    

    Where would you benefit from it:

    public Matrix {
      private Dictionary<string, double> matrixData; // Matrix indecies, value
      public double this[int row, int col] {
        get {
          return matrixData[string.Format("{0},{1}", row, col)];
        }
      }
    }
    

    As you can see, for some reason, your data is a Dictionary indexed with a string key and you wish to call this with two integer indecies and still do not want to change your data type:

    Matrix m = new Matrix();
    ...
    Console.WriteLine( m[1,2] );
    
    0 讨论(0)
提交回复
热议问题