Search code examples
c#interface

How to define Indexer behaviour to an Interface?


Is it possible to add the indexer behaviour from an interface?

something like this :

interface IIndexable<T>
{
   T this[string index];
}

Solution

  • Yes, it is possible. In fact, all you're missing is the getter/setter on your indexer. Just add it as follows:

    interface IIndexable<T>
    {
         T this[string index] {get; set;}
    }