Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 311
Francesco Belladonna Avatar asked Oct 02 '11 11:10

Francesco Belladonna


2 Answers

Of course:

public abstract class ClassWithAbstractIndexer 
{
    public abstract int this[int index]
    {
        get;
        set;
    }
}
like image 65
Amittai Shapira Avatar answered Nov 15 '22 20:11

Amittai Shapira


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

like image 41
Henk Holterman Avatar answered Nov 15 '22 18:11

Henk Holterman