What you are doing is called an indexer. You can add an indexer to classes. It usually takes 1 or more arguments.
Calling an indexer:
To call an indexer, you use an instance of that class and add []
at the end of the name. Then, add the arguments into the []
. Let's take string as an example. In the string class, there is an indexer that takes a argument of type int
. It gets the character at that index of the string.
char theFirstChar = someString[0];
The indexer can also take multiple arguments:
int[,] matrix = new int[10, 10]; //Note: This is not an indexer
int someValue = matrix[9, 4]; //This is
Syntax:
You define an indexer like this: (I used the string example)
public char this[int i]
{
get
{
// code
}
set
{
// code
}
}
It's very much like a property.