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 a class, Interface can have methods, properties, events, and indexers as its members.
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.
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.
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.
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; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With