Behind the Scenes Look at C#: Indexers
(Page 1 of 4 )
Indexers help you to organize your data and get to it more quickly. In order to use one, however, there are certain steps we must take. Michael Youssef details those steps in this article.
An index is an abstract concept. In order to gain faster access to some data that you have (in a form of object or any other construction), you index the data. In C# an index is a construction (actually a property as we will see soon) that supports the access to an internal collection (maybe an array or a collection class) of data about a given object. For example, any Department contains more than one Employee. When we want to write the Department and the Employee class, we need a way to describe that the Department class can have many Employee instances (Employees) and the client code can access these internal instances in a common way. In this situation we need an indexer to do the job.
There are steps that we need to take. First we need to define the internal storage mechanism for the Employee instances in the Department class (we can do that by using a simple array or a complex collection class). Note that without the internal array or collection the indexer doesn't make any sense; its role to give the client code access to the internal array. Second, we need to write the indexer to provide the client code with an access mechanism. The indexer looks like this:
public Employee this[int arg]
{
get
{
return this.Employees[arg];
}
set
{
this.Employees[arg] = value;
}
}
As you can see, the indexer defines both the get and set accessors as much as a property does, and it uses the implicit argument value in the same way that a set accessor uses it. In this example, we define the indexer using the "this" keyword. The indexer's parameter is the index that you will use to get and set the value into the internal array, as we will see shortly.
The indexer uses the keyword this to refer to the current instance of the Department object. This makes sense because it's as if we are saying "put this Employee instance in me." "Me" in this case refers to the Department object, and the indexer is the default property of the object, so "me" means "in my indexer." A class can have only one indexer, but we can overload this indexer to accept varies parameters.
In our example, the class Department is considered to be a container class because it contains a collection of other objects and, as you will see, the relationship between the Department class and the Employee class is one-to-many. There are many Employee instances in the Department instance, and the Department instance has more than one Employee instance. This is similar to one-to-many relationships in relational database systems.
Using indexers, we will use the index operator ([]) with our user-defined classes (the Department class in this example) in the same way as we use the index operator with the array to access its individual members. We are going to see that in the example.
Let's take a look at our first indexer example.
Next: C# Indexers Example (First Iteration) >>
More C# Articles
More By Michael Youssef