The [] operator is called an indexer. You can provide indexers that take an integer, a string, or any other type you want to use as a key. The syntax is straightforward, following the same principles as property accessors.
For example, in your case where an int
is the key or index:
public int this[int index]
{
get
{
return GetValue(index);
}
}
You can also add a set accessor so that the indexer becomes read and write rather than just read-only.
public int this[int index]
{
get
{
return GetValue(index);
}
set
{
SetValue(index, value);
}
}
If you want to index using a different type, you just change the signature of the indexer.
public int this[string index]
...