Francesco Belladonna
Francesco Belladonna

Reputation: 11689

Any possibility to declare indexers in C# as an abstract member?

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?

Upvotes: 13

Views: 3230

Answers (3)

user586399
user586399

Reputation:

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.

Upvotes: 0

Amittai Shapira
Amittai Shapira

Reputation: 3827

Of course:

public abstract class ClassWithAbstractIndexer 
{
    public abstract int this[int index]
    {
        get;
        set;
    }
}

Upvotes: 21

Henk Holterman
Henk Holterman

Reputation: 273274

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

Upvotes: 2

Related Questions