7

In C#, what's the syntax for declaring an indexer as part of an interface? Is it still this[ ]? Something feels odd about using the this keyword in an interface.

Dinah
  • 52,922
  • 30
  • 133
  • 149

3 Answers3

20
public interface IYourList<T>
    {
        T this[int index] { get; set; }
    }
Stan R.
  • 15,757
  • 4
  • 50
  • 58
5

It is - it's pretty odd syntax at other times if you ask me! But it works. You have to declare the get; and/or set; parts of it with no definition, just a semi-colon, exactly like ordinary properties in an interface.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
4

I know what you mean but, yes, this is correct. Here are the docs.

Ben Griswold
  • 17,793
  • 14
  • 58
  • 60