Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface that Entails the Implementation of Indexer

Tags:

c#

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?

like image 779
Graviton Avatar asked Mar 20 '10 11:03

Graviton


People also ask

Can an interface have indexers?

Like a class, Interface can have methods, properties, events, and indexers as its members.

What statement do you use to implement an indexer?

To declare an indexer for a class, you add a slightly different property with the this[] keyword and arguments of any type between the brackets. Properties return or set a specific data member, whereas indexers return or set a particular value from the object instance.

What is an indexer What is it used for?

Indexers are a syntactic convenience that enable you to create a class, struct, or interface that client applications can access as an array. The compiler will generate an Item property (or an alternatively named property if IndexerNameAttribute is present), and the appropriate accessor methods.

What is the use of indexer write the syntax for indexer?

A read-only indexer can be implemented as an expression-bodied member, as the following example shows. using System; class SampleCollection<T> { // Declare an array to store the data elements. private T[] arr = new T[100]; int nextIndex = 0; // Define the indexer to allow client code to use [] notation.


1 Answers

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; }
}
like image 105
Codesleuth Avatar answered Oct 07 '22 20:10

Codesleuth