DataGridView, for example, lets you do this:
DataGridView dgv = ...;
DataGridViewCell cell = dgv[1,5];
but for the life of me I can't find the documentation on the index/square-bracket operator. What do they call it? Where is it implemented? Can it throw? How can I do the same thing in my own classes?
ETA: Thanks for all the quick answers. Briefly: the relevant documentation is under the "Item" property; the way to overload is by declaring a property like public object this[int x, int y]{ get{...}; set{...} }
; the indexer for DataGridView does not throw, at least according to the documentation. It doesn't mention what happens if you supply invalid coordinates.
ETA Again: OK, even though the documentation makes no mention of it (naughty Microsoft!), it turns out that the indexer for DataGridView will in fact throw an ArgumentOutOfRangeException if you supply it with invalid coordinates. Fair warning.
Source of the information
For bracket:
BUT
The array indexing operator cannot be overloaded; however, types can define indexers, properties that take one or more parameters. Indexer parameters are enclosed in square brackets, just like array indices, but indexer parameters can be declared to be of any type (unlike array indices, which must be integral).
From MSDN
If you mean the array indexer,, You overload that just by writing an indexer property.. And you can overload, (write as many as you want) indexer properties as long as each one has a different parameter signature
you can find how to do it here. In short it is:
If you're using C# 6 or later, you can use expression-bodied syntax for get-only indexer:
public object this[int i] => this.InnerList[i];
That would be the item property: http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx
Maybe something like this would work: