59

I was going through some library code and saw a method like:

public CollapsingRecordNodeItemList List
{
    get { return this[0] as CollapsingRecordNodeItemList; }
}

The class that contains this method is not a list or something iterable, so what exactly does this[0] mean?

Smi
  • 13,850
  • 9
  • 56
  • 64
Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169
  • 15
    the class has an [indexer](http://msdn.microsoft.com/en-us/library/vstudio/6x16t2tx.aspx) – Paolo Falabella May 20 '13 at 13:07
  • 10
    Does the class override the index operator []? – Nick May 20 '13 at 13:08
  • @Nick I just saw that one of the parent classes has a line like `public GoObject this[string name] { get; set; }` maybe its related to this ? – Cemre Mengü May 20 '13 at 13:10
  • There'll be another one which is indexed on integers. – Nick May 20 '13 at 13:11
  • 1
    @Cemre see if there is a `this[int index]` or `this[long index]` or something like that... you can't pass an integer (`0`) in as `string name`. – Marc Gravell May 20 '13 at 13:12
  • 2
    @Cemre, thats just another indexer. The parent class is indexed with the `string` type. – Jodrell May 20 '13 at 13:12
  • 1
    Marc is correct.. it is the same idea - it is an indexer but it is not the one your line is referring.. – G.Y May 20 '13 at 13:14
  • Yeah as you said there was another one: `public virtual GoObject this[int index] { get; set; }`. Thanks for the comments! – Cemre Mengü May 20 '13 at 13:26
  • 2
    Just an additional piece of information that might help future readers. Indexers were first introduced in the abstract class Collectionbase. This feature was later also made a part of List. – developer747 May 20 '13 at 18:38

4 Answers4

96

Look for an indexer in the class.

C# lets you define indexers to allow this sort of access.

Here is an example from the official guide for "SampleCollection".

public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets 
            // the corresponding element from the internal array. 
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }

Here is the definition from the official language specification:

An indexer is a member that enables objects to be indexed in the same way as an array. An indexer is declared like a property except that the name of the member is this followed by a parameter list written between the delimiters [ and ]. The parameters are available in the accessor(s) of the indexer. Similar to properties, indexers can be read-write, read-only, and write-only, and the accessor(s) of an indexer can be virtual.

One can find the full and complete definition in section 10.9 Indexers of the specification.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I discovered a property in one of the parent classes but the index is `string` instead of `int` : `public GoObject this[string name] { get; set; }` . I guess there is another one somewhere else. – Cemre Mengü May 20 '13 at 13:12
  • 4
    @Cemre The language specification states that (section 1.6.7.3) "Indexers can be overloaded, meaning that a class can declare multiple indexers as long as the number or types of their parameters differ." :) This might be the case here – Benjamin Gruenbaum May 20 '13 at 13:13
  • 1
    Thanks for the comment. Just found the other one which was: `public virtual GoObject this[int index] { get; set; }` :) – Cemre Mengü May 20 '13 at 13:23
10

It means that the declaring type (or a base-class of that) has an "indexer" which presumably takes an int (or similar) and returns... something (perhaps object ?). The code calls the indexer's get accessor, passing 0 as the index - and then treats the returned value as a CollapsingRecordNodeItemList (or null the returned value isn't compatible with that).

For example:

public object this[int index] {
    get { return someOtherList[index]; }
}

Easiest thing to do is the step into it, though. That will tell you exactly where it is going.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

Assuming the class itself inherits from some form if IList/IList<T>, it's just returning (and casting) the first element in the collection.

public class BarCollection : System.Collections.CollectionBase
{
    public Bar FirstItem
    {
        get { return this[0] as Bar; }
    }

    #region Coming From CollectionBase
    public Object this[ int index ]  {
        get { return this.InnerList[index]; }
        set { this.InnerList[index] = value; }
    }
    #endregion
}
user
  • 6,897
  • 8
  • 43
  • 79
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
1

It means to invoke the item property's get method on this class. It's called the class's Indexer

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448