11

As the title states, I would like to declare an indexer object this[int index] in an abstract class as an abstract member.

Is this possible in any way? Is it possible to declare this in an interface too?

nawfal
  • 70,104
  • 56
  • 326
  • 368
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147

3 Answers3

19

Of course:

public abstract class ClassWithAbstractIndexer 
{
    public abstract int this[int index]
    {
        get;
        set;
    }
}
xanatos
  • 109,618
  • 12
  • 197
  • 280
Amittai Shapira
  • 3,749
  • 1
  • 30
  • 54
2

A simple example:

public interface ITest
{
    int this[int index] { get;  }
}

public class Test : ITest
{
    public int this[int index]
    {
        get { ... }
        private set { .... }
    }
}

Several combinations of private/protected/abstract are possible for get and set

H H
  • 263,252
  • 30
  • 330
  • 514
0

You may declare it like this:

internal abstract class Hello
{
  public abstract int Indexer[int index]
  {
      get;
  }
}

Then you'll have the option to override only get or override both get and set.