7

I am looking for a framework-defined interface that declares indexer. In other words, I am looking for something like this:

public interface IYourList<T>
{
    T this[int index] { get; set; }
}

I just wonder whether .Net framework contains such interface? If yes, what is it called?

You might ask why I can't just create the interface myself. Well, I could have. But if .Net framework already has that why should I reinvent the wheel?

Community
  • 1
  • 1
Graviton
  • 81,782
  • 146
  • 424
  • 602

3 Answers3

5

I think you're looking for IList<T>.

Sample pasted from the MSDN site:

T this[
    int index
] { get; set; }

EDIT MORE:

This is the entire class I just Reflected to show you exactly how the interface is described in the framework:

[TypeDependency("System.SZArrayHelper")]
public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
    // Methods
    int IndexOf(T item);
    void Insert(int index, T item);
    void RemoveAt(int index);

    // Properties
    T this[int index] { get; set; }
}
Codesleuth
  • 10,321
  • 8
  • 51
  • 71
0

I am not aware of such an interface in the BCL.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

There is no interface that only implements an indexer (generic or otherwise).

The closest you can get is use some of the collection interfaces, such as IList.

Oded
  • 489,969
  • 99
  • 883
  • 1,009