In C#, what's the syntax for declaring an indexer as part of an interface? Is it still this[ ]? Something feels odd about using the this keyword in an interface.
Indexers can be declared on an interface. Accessors of interface indexers differ from the accessors of class indexers in the following ways: Interface accessors do not use modifiers.
Like a class, Interface can have methods, properties, events, and indexers as its members.
Indexers allow instances of a class or struct to be indexed just like arrays. The indexed value can be set or retrieved without explicitly specifying a type or instance member. Indexers resemble properties except that their accessors take parameters.
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.
public interface IYourList<T>
{
T this[int index] { get; set; }
}
It is - it's pretty odd syntax at other times if you ask me! But it works. You have to declare the get;
and/or set;
parts of it with no definition, just a semi-colon, exactly like ordinary properties in an interface.
I know what you mean but, yes, this is correct. Here are the docs.
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