I’ve seen/used objects within the .NET Framework that have default indexer properties (ie: SqlDataReader, System.Collections.Generic.List). Now how exactly do I give my own custom object type a default indexer property?

Well, it’s actually rather simple. Heres an example of a Club object with a People collection of type Person that has a default indexer property set up:<FONT color=#0000ff size=2>

public</FONT> <FONT color=#0000ff size=2>class</FONT> <FONT color=#2b91af size=2>Club
</FONT>{
<FONT color=#808080 size=2> ///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><summary>
</FONT><FONT color=#808080 size=2> ///</FONT><FONT color=#008000 size=2> Collection of Person objects in the Club
</FONT><FONT color=#808080 size=2> ///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2></summary>
</FONT><FONT color=#0000ff size=2> private</FONT> <FONT color=#2b91af size=2>List</FONT><<FONT color=#2b91af size=2>Person</FONT>> _people = <FONT color=#0000ff size=2>new</FONT> <FONT color=#2b91af size=2>List</FONT><<FONT color=#2b91af size=2>Person</FONT>>();
<FONT color=#0000ff size=2> public</FONT> <FONT color=#2b91af size=2>List</FONT><<FONT color=#2b91af size=2>Person</FONT>> People
{
<FONT color=#0000ff size=2> get</FONT> { <FONT color=#0000ff size=2>return</FONT> _people; }
<FONT color=#0000ff size=2> set</FONT> { _people = <FONT color=#0000ff size=2>value</FONT>; }
}

</FONT><FONT color=#808080 size=2> ///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><summary>
</FONT><FONT color=#808080 size=2> ///</FONT><FONT color=#008000 size=2> The Default property of the Club object
</FONT><FONT color=#808080 size=2> ///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2></summary>
</FONT><FONT color=#0000ff size=2> public</FONT> <FONT color=#2b91af size=2>Person</FONT> <FONT color=#0000ff size=2>this</FONT>[<FONT color=#0000ff size=2>int</FONT> index]
{
<FONT color=#0000ff size=2> get</FONT> { <FONT color=#0000ff size=2>return</FONT> _people[index]; }
<FONT color=#0000ff size=2> set</FONT> { _people[index] = <FONT color=#0000ff size=2>value</FONT>; }
}
}

</FONT><FONT color=#0000ff size=2>public</FONT> <FONT color=#0000ff size=2>class</FONT> <FONT color=#2b91af size=2>Person
</FONT>{
<FONT color=#0000ff size=2> public</FONT> Person(<FONT color=#0000ff size=2>string</FONT> name)
{
<FONT color=#0000ff size=2> this</FONT>._name = name;
}

</FONT><FONT color=#0000ff size=2> private</FONT> <FONT color=#0000ff size=2>string</FONT> _name;
<FONT color=#0000ff size=2> public</FONT> <FONT color=#0000ff size=2>string</FONT> Name
{
<FONT color=#0000ff size=2> get</FONT> { <FONT color=#0000ff size=2>return</FONT> _name; }
<FONT color=#0000ff size=2> set</FONT> { _name = <FONT color=#0000ff size=2>value</FONT>; }
}
}

And, here’s example code of using that object:<FONT color=#008000 size=2>

// Create instance of Club object
</FONT><FONT color=#2b91af size=2>Club</FONT> club = <FONT color=#0000ff size=2>new</FONT> <FONT color=#2b91af size=2>Club</FONT>();

</FONT><FONT color=#008000 size=2>// Add a couple Person objects to the Peopl collection of the Club
</FONT>club.People.Add(<FONT color=#0000ff size=2>new</FONT> <FONT color=#2b91af size=2>Person</FONT>(<FONT color=#a31515 size=2>”Chris”</FONT>));
club.People.Add(
<FONT color=#0000ff size=2>new</FONT> <FONT color=#2b91af size=2>Person</FONT>(<FONT color=#a31515 size=2>”Kate”</FONT>));

</FONT><FONT color=#008000 size=2>// Change the name of the first Person in the People
</FONT><FONT color=#008000 size=2>// collection using the traditional method
</FONT>club.People[0].Name = <FONT color=#a31515 size=2>”Joe”</FONT>;

</FONT><FONT color=#008000 size=2>// Change the name of the first Person in the People
</FONT><FONT color=#008000 size=2>// collection using the Club objects Default property
</FONT>club[0].Name = <FONT color=#a31515 size=2>”John”</FONT>;

And, remember just like any other method/property the Default Indexer Property can be overloaded. For instance, you can define one that accepts an Index as type integer, and also have one that accepts an Index of type string that does a dictionary lookup.

**Update 3/23/2007: **I have updated this post and changed the title, since there seems to be some confusion as to what I’m describing is a Default Property or Indexer Property. I think I’ll just refer to it as a Default Indexer Property. Sorry for the confusion.

For further reference: MSDN: Default Properties for your Components