Classes

Properties and Indexers

Int Indexers

Sometimes it will be neccesary to access to a collection within a class. For this purpose there is a special kind of property, called Indexer, which will let us to access those members as if the current instance class were an array.

The signature of an indexer must be:

 public type this [index-type] {

	get {}

	set {}

 }
The type is refering to the type of the collection. For example, an int array, would have an indexer of type int. The keyword this is refering to the actual object ( note that when you use the indexer, you are using the brackets with the name of the current instance ). And index-type is the type of the index ( most of the times an int, but it is also possible to use a string, for example ). In this section we will see those indexers that receive an int as index.

Let's write a small sample:

//
// A first approach to the Indexers
//
using System;

 public class IndexerTest {

	// Private int[]
	private int[] array;

	// Constructor
	public IndexerTest ( int elements )
	{

		this.array = new int[elements];

	}

	// Indexer
	public int this [ int index ] {

		get {
			return this.array[index];
		}	

		set {
			this.array[index] = value;
		}

	}

 }

 public class Test {

	public static void Main () {
	
		// We will use 10 elements
		IndexerTest test = new IndexerTest(10);

		// Assing some values
		test[0] = 5;
		test[6] = 45;
		test[9] = 67;
	
		// Let's show the values
		for ( int i = 0; i 10; i++ )
		{
			Console.WriteLine(" test[i] = {0}", test[i]);
		}
	
	 }

 }

Save, compile and run it. You should get the next message:

 test[i] = 5
 test[i] = 0
 test[i] = 0
 test[i] = 0
 test[i] = 0
 test[i] = 0
 test[i] = 45
 test[i] = 0
 test[i] = 0
 test[i] = 67

Now it's time to observe the Indexer more carefully. The indexer for this class is defined as

 // Indexer
 public int this [ int index ] {

	get {
		return this.array[index];
	}	

	set {
		this.array[index] = value;
	}

 }
Int the get section is defined what values will be retrieving. In this case, the value of the array[index] value. Next, in the set section is defined how the indexer will receive the assigments: the array[index] element will have the value passed. This look very similar to a common property. However, there is a pair of special aspects: the first, is that there's not any Length property defined. Because of this, in the for statement the condition depends on a constant, the number 10. This problem can be solved adding the next property to the IndexerTest class:
 public int Length {

	// Retrieve the Length property of the current member collection
	// In this case, vector[]
	get {
		return this.vector.Length;
	}

 }
And now it is possible to change the for statement:
 for ( int i = 0; i test.Length; i++) {
	...
 }
And the second aspect is that some times it is possible to pass a wrong index to the indexer. For example, if you pass a -1 value to the indexer ( test[-1] ) you will have problem, because a Exception will be thrown. It is possible to handle these kind of errors or let the environment throw this kind excpetions and handle them as they were really arrays.