Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

public SomeClass {
  public someInterface this[String strParameter] {
    get {
      return SomeInternalMethod(strParameter);
    }
  }
}

It looks like it is called as follows:

SomeClass _someClass = new SomeClass();
SomeInterface returnedValue = _someClass["someString"];

I am interested in where this function would be appropriate or what the intent of writing in this style. For example why would this be preferred over simply calling the function?

like image 203
Michael Gattuso Avatar asked Jan 06 '10 22:01

Michael Gattuso


3 Answers

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.
like image 165
Eric Lippert Avatar answered Oct 20 '22 00:10

Eric Lippert


It seems like a lot of the answers are focusing on what an indexer is, not why you would want to use one.

As far as I'm concerned, here is the motivation to use an indexer:

You are working on a class that has a collection of some sort, but you want the class to appear to users (consumers of the class) as if it is a collection.

The best example I can think of is the DataRow class in ADO.NET. If you want to get the value of the fifth cell of a DataRow, you can either use DataRow.Item[4] or DataRow[4]. The latter form is a convenient and logical shortcut, and it shows off pretty nicely why you'd want to use an indexer. To the user, the DataRow can be thought of as just a collection of cells (even though it is really more than that), so it makes sense to be able to get and set cell values directly, without having to remember that you are actually getting/setting an Item.

Hope that helps.

like image 36
devuxer Avatar answered Oct 19 '22 22:10

devuxer


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

like image 39
Erich Avatar answered Oct 20 '22 00:10

Erich