309

I would like to add an operator to a class. I currently have a GetValue() method that I would like to replace with an [] operator.

class A
{
    private List<int> values = new List<int>();

    public int GetValue(int index) => values[index];
}
AustinWBryan
  • 2,968
  • 3
  • 17
  • 35
Adam Tegen
  • 23,348
  • 32
  • 115
  • 149

4 Answers4

795
public int this[int key]
{
    get => GetValue(key);
    set => SetValue(key, value);
}
AustinWBryan
  • 2,968
  • 3
  • 17
  • 35
Florian Greinacher
  • 13,628
  • 1
  • 31
  • 50
  • 179
    Why is it that every time I need to implement an index operator, I have to look it up? And each time I end up on this answer... wish I could vote it up multiple times :) – DSO Dec 31 '11 at 00:52
  • 7
    This is so super awesome. Can it be done in an interface? ``interface ICache { object this[string key] { get; set; } }`` **Edit:** [Yes.](http://stackoverflow.com/questions/9586625/how-to-define-indexer-behaviour-to-an-interface) – Michael Sep 19 '12 at 20:45
  • 19
    dont know why they chose to omit the word 'operator' in this declaration - thats the mistake I always make! Nice answer – JonnyRaa Apr 22 '13 at 13:56
  • 3
    Michael: You should probably be using generics: `interface ICache { TContent this[string key] { get; set; } }`. – cemper93 Jan 16 '16 at 16:01
  • 12
    I just want to say hi to myself in the future, he'll be back here for sure – qwertoyo Jul 06 '18 at 13:19
  • You can also use multi dimensional array `public int this[int x, int y]` – Paul Baxter Feb 28 '20 at 19:09
67

I believe this is what you are looking for:

Indexers (C# Programming Guide)

class SampleCollection<T>
{
    private T[] arr = new T[100];
    public T this[int i]
    {
        get => arr[i];
        set => arr[i] = value;
    }
}

// This class shows how client code uses the indexer
class Program
{
    static void Main(string[] args)
    {
        SampleCollection<string> stringCollection = 
            new SampleCollection<string>();
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}
AustinWBryan
  • 2,968
  • 3
  • 17
  • 35
William Brendel
  • 30,014
  • 14
  • 69
  • 77
30

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 => 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 => 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]
...
AustinWBryan
  • 2,968
  • 3
  • 17
  • 35
Jeff Yates
  • 58,658
  • 18
  • 135
  • 183
9
public int this[int index]
{
    get => values[index];
}
AustinWBryan
  • 2,968
  • 3
  • 17
  • 35
BFree
  • 97,931
  • 20
  • 150
  • 197