An indexer enables you to use an index on an object to obta in values stored with in the object. in essence this enables you to treat an object like an array.
An indexer is also similar to a property. As with properties, you use get and set when def in ing an indexer. Unlike properties, you are not obta in ing a specific data member; rather, you are obta in ing a value from the object itself. When you def ine a property, you def ine a property name. With indexers, instead of creat ing a name as you do with properties, you use the this keyword, which refers to the object instance and thus the object name is used. The format for def in ing an indexer is
public dataType this[ int index]
{
get
{
// Do whatever you want...
return aValue;
}
set
{
// Do whatever you want
// Generally you should set a value with in the class
// based on the index and the value they assign.
}
}
Creat ing an indexer enables you to use bracket notation ([]) with an object to set and get a value from an object. As you can see in the format shown earlier, you state the dataType that will be set and returned by the indexer. in the get section, you return a value that is of dataType. in the set block, you will be able to someth ing with a value of dataType. As with properties and member functions, you can use the value keyword. This is the value passed as the argument to the set rout ine. List ing 1 presents a simple example of Us ing an indexer.
List ing 1. Us ing an indexer
1: // indx2.cs - Us ing an indexer
2: //--------------------------------------------------
3:
4: Us ing System;
5:
6: public class Spell ingList
7: {
8: protected str ing[] words = new str ing[size];
9: static public int size = 10;
10:
11: public Spell ingList()
12: {
13: for ( int x = 0; x < size; x++ )
14: words[x] = Str ing.Format("Word{0}", x);
15: }
16:
17: public str ing this[ int index]
18: {
19: get
20: {
21: str ing tmp;
22:
23: if( index >= 0 && index <= size-1 )
24: tmp = words[ index];
25: else
26: tmp = "";
27:
28: return ( tmp );
29: }
30: set
31: {
32: if( index >= 0 && index <= size-1 )
33: words[ index] = value;
34: }
35: }
36: }
37:
38: public class TestApp
39: {
40: public static void Ma in()
41: {
42: Spell ingList myList = new Spell ingList();
43:
44: myList[3] = "=====";
45: myList[4] = "Brad";
46: myList[5] = "was";
47: myList[6] = "Here!";
48: myList[7] = "=====";
49:
50: for ( int x = 0; x < Spell ingList.size; x++ )
51: Console.WriteL ine(myList[x]);
52: }
53: }
The output from this list ing is:
Word0
Word1
Word2
=====
Brad
was
Here!
=====
Word8
Word9
This list ing creates an indexer to be used with the Spell ingList class. The Spell ingList class conta ins an array of str ings called words that can be used to store a list of words. This list is set to the size of the variable declared in l ine 9.
L ines 11 to 15 conta in a constructor for Spell ingList that sets initial values into each element of the array. You could just as easily have requested the words from the reader or read them from a file. This constructor assigns the str ing value Word## to each of the elements in the array, where ## is the element number of the array.
Jump ing down to the TestApp class in l ines 38 to 53, you see how the Spell ingList class is go ing to be used. in l ine 42, the Spell ingList class is used to instantiate the myList object that will hold the words. L ine 42 also causes the constructor to be executed. This initializes the Word## values. L ines 44 to 48 then change some of these values.
If you th ink back to how you worked with arrays, you should be say ing, "wait a m inute" as you look at l ines 44 to 48. To access the value of one of the words, you would normally have to access the data member with in the object. When Us ing arrays as a data member, you learned that you would assign a value to the fourth element as
MyList.words[3] = "=====";
L ine 44, however, is access ing the fourth element with in the object, which has been set to be the fourth element in the words array.
An indexer has been created for the Spell ingList class in l ines 17 to 35. This indexer enables you to access the elements with in the words array Us ing just the object name.
L ine 17 is the def in ing l ine for the indexer. You know this is an indexer rather than a property because the this keyword is used instead of a name. Additionally, this is given an index (called index). The indexer will return a str ing value.
L ines 19 to 29 conta in the get portion of the indexer. The get block returns a value based on the index. in this class, this value is an element from the words array. You can return any value you want. The value should, however, make sense. in l ine 23, a check is done to make sure the index value is valid. If you don't check the value of the index, you risk hav ing an exception thrown. in this list ing, if the index is out of the range, a null value is returned (l ine 26). If the index is valid, the value stored in the words array at the index location will be returned.
The set portion of the indexer is in l ines 30 to 34. This block can be used to set information with in the object. As with properties, the value keyword conta ins the value be ing assigned. in this code, the index value is aga in checked to make sure it is valid. If it is, a word in the words array will be updated at the index location with the value assigned.
Look ing aga in at the test class, you see that the set indexer block is used to assign values in l ines 44 to 48. For l ine 44, the set indexer block will be called with value equal to ===== and will be passed with an index value of 3. in l ine 45, value will be Brad and the index is 4. in l ine 51, the get indexer block is called with an index value of x. The value returned will be the str ing value returned by the get indexer block.
There are times to use indexers and there are times to not use them. You should use indexers when it makes your code more readable and easier to understand. For example, you can create a stack that can have items placed on it and taken off of it. Us ing an indexer, you could access items with in the stack without need ing to remove items.