You can think of the indexer as an object that takes a parameter. What you do with this option is up to you.
Typically, indexers are used where provided, well, the index for your object makes sense.
For example, if your object deals with collections of elements, the ability to access them using the simple '[]' syntax makes sense, because just thinking of it as an array, to which we give the position of the object we want to return.
I recently wrote a blog post about aspects of indexers that use reflection .
One example that I gave was:
using System; using System.Collections; public class DayPlanner {
And then you can use it as follows:
DayPlanner myDay = new DayPlanner(); myDay[DateTime.Parse("2006/02/03")] = "Lunch"; ... string whatNow = myDay["2006/06/26"];
To show that you can flip the indexer target to do what you want.
This does not mean that you should ... :-)
Renaud bompuis
source share