Although operator overloading is possible in C# (just like in C++), overloading the array square bracket operator is by definition not possible. However, C# provides an alternative called Indexers (C# Programming Guide).
This is directly from the Microsoft C# Reference for the [] Operator:
The array indexing operator cannot be overloaded; however, types can define indexers, and properties that take one or more parameters. Indexer parameters are enclosed in square brackets, just like array indexes, but indexer parameters can be declared to be of any type, unlike array indexes, which must be integral.
The syntax for indexers resembles the C# syntax for properties rather than its syntax for operator overloading. Here's a quick snippet for providing array-like access to a List you'd like to keep hidden within a class:
Worked for me, and hopefully it works just as well for you!
UPDATE: As I do a quick search now, this Stack Overflow post just turned up.
Comments 2
Thanks, that worked great. Plus the stackoverflow link helped too. Cheers mate!
As of today, the solution would be the single-line one explained in this SO answer: https://stackoverflow.com/questions/287928/how-do-i-overload-the-square-bracket-operator-in-c/34098286#34098286