How do I overload the square-bracket operator in C#?
That would be the item property: http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx
Maybe something like this would work:
public T Item[int index, int y]
{
//Then do whatever you need to return/set here.
get; set;
}
Operators Overloadability
+, -, *, /, %, &, |, <<, >> All C# binary operators can be overloaded.
+, -, !, ~, ++, --, true, false All C# unary operators can be overloaded.
==, !=, <, >, <= , >= All relational operators can be overloaded,
but only as pairs.
&&, || They can't be overloaded
() (Conversion operator) They can't be overloaded
+=, -=, *=, /=, %= These compound assignment operators can be
overloaded. But in C#, these operators are
automatically overloaded when the respective
binary operator is overloaded.
=, . , ?:, ->, new, is, as, sizeof These operators can't be overloaded
[ ] Can be overloaded but not always!
Source of the information
For bracket:
public Object this[int index]
{
}
##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're using C# 6 or later, you can use expression-bodied syntax for get-only indexer:
public object this[int i] => this.InnerList[i];
you can find how to do it here. In short it is:
public object this[int i]
{
get { return InnerList[i]; }
set { InnerList[i] = value; }
}
If you only need a getter the syntax in answer below can be used as well (starting from C# 6).