271

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.

Coderer
  • 25,844
  • 28
  • 99
  • 154

8 Answers8

410

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).

Ruben
  • 6,367
  • 1
  • 24
  • 35
  • 9
    a minor comment: depending on what you're doing, you might find it more appropriate to do: get { return base[i]; } set { base[i] = value; } – MikeBaz - MSFT Oct 04 '12 at 21:58
  • 9
    This isn't operator overloading. It is indexer – Destructor May 12 '16 at 08:32
  • 6
    Indexer, may as well be a unary operator. – alan2here Aug 09 '16 at 12:27
  • 1
    In 2019, a new answer should be selected, [this one](https://stackoverflow.com/a/34098286/774575). Too bad SO doesn't have a feature to deal with deprecated answers, as the new one is not close to get 350+ upvotes, though it deserves them. – mins Jul 03 '19 at 16:52
  • @mins I included a link to the other answer. – Ruben Jul 08 '19 at 01:01
44

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; 
}
Ricardo Villamil
  • 5,031
  • 2
  • 30
  • 26
  • Thanks a lot! If I could set two answers, I'd add yours as well -- nobody else knew to look for Item in the documentation... – Coderer Nov 13 '08 at 19:34
  • 5
    In terms of how it gets implemented, it is "Item", but in C# terms, it is "this" – Marc Gravell Nov 13 '08 at 21:43
  • 1
    Right, but I asked "for the life of me I can't find the documentation on the index/square-bracket operator" -- I meant when you look up a library class in MSDN, where do they tell you about the operator? That's why I made that last "ETA" about what it throws -- the docs are *wrong*. – Coderer Nov 14 '08 at 14:59
28
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

dimitar.bogdanov
  • 387
  • 2
  • 10
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
17

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];

amoss
  • 1,571
  • 1
  • 15
  • 27
5
public class CustomCollection : List<Object>
{
    public Object this[int index]
    {
        // ...
    }
}
Jason Miesionczek
  • 14,268
  • 17
  • 76
  • 108
  • 6
    Actually, this is really dangerous - you now have two competing implementations: anyone with a variable typed as List or IList or IList etc won't execute your custom code. – Marc Gravell Nov 13 '08 at 21:35
  • 1
    Agreed -- if nothing else, it's not necessary to derive your CustomCollection from List, but I hadn't realized it was actually dangerous – Coderer Nov 14 '08 at 14:58
  • It will still execute the custom code though, won't it? It doesn't matter what variable type you declare - it's the type of the object that matters. – izb Nov 11 '10 at 15:53
  • If you pass the object to a function accepting List then the custom code is not called – David Sykes Feb 14 '12 at 09:07
  • 1
    Friendly reminder of C# polymorphism: That happens because the base class doesn't declare its implementation as virtual. If it were declared as virtual, the custom code would be called in every case. – almulo May 20 '15 at 12:03
  • 1
    Also, this code will give you a compiler warning since both indexers are non-virtual. The compiler will suggest that you qualify your custom indexer with the `new` keyword. – amoss May 18 '16 at 18:48
4

Here is an example returning a value from an internal List object. Should give you the idea.

  public object this[int index]
  {
     get { return ( List[index] ); }
     set { List[index] = value; }
  }
Rob Prouse
  • 22,161
  • 4
  • 69
  • 89
3

For CLI C++ (compiled with /clr) see this MSDN link.

In short, a property can be given the name "default":

ref class Class
{
 public:
  property System::String^ default[int i]
  {
    System::String^ get(int i) { return "hello world"; }
  }
};
1

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

public class EmployeeCollection: List<Employee>
{
    public Employee this[int employeeId]
    {   
        get 
        { 
            foreach(var emp in this)
            {
                if (emp.EmployeeId == employeeId)
                    return emp;
            }

            return null;
        }
    }

    public Employee this[string employeeName]
    {   
        get 
        { 
            foreach(var emp in this)
            {
                if (emp.Name == employeeName)
                    return emp;
            }

            return null;
        }
    }
}
vzwick
  • 11,008
  • 5
  • 43
  • 63
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • 2
    First, you mean this[], not this() - however, providing a custom (but different) this[int] on something that is a list (IList/IList/List) is quite dangerous - and could lead to subtle bugs between the "int index" and "int employeeId" versions. Both are still callable. – Marc Gravell Nov 13 '08 at 21:40
  • and in fact, I don;t think the code I entered above would compile without adding a new or override statement precisely because of the existence of the List implementation of this[int]. You're right about the potential when doing this, simply meant it as example of overloading indexer. – Charles Bretana Nov 13 '08 at 21:48