An indexer is a member that enables an object to be indexed in the same way as an array. Indexers are declared using indexer-declarations:
- attributesopt indexer-modifiersopt indexer-declarator
- accessor-declarations
- indexer-modifier
- indexer-modifiers indexer-modifier
- new
- public
- protected
- internal
- private
- virtual
- override
- abstract
- type this [ formal-parameter-list ]
- type interface-type . this [ formal-parameter-list ]
The formal-parameter-list specifies the parameters of the indexer. The formal parameter list of an indexer corresponds to that of a method , except that at least one parameter must be specified, and that the ref and out parameter modifiers are not permitted. The type of an indexer and each of the types referenced in the formal-parameter-list must be at least as accessible as the indexer itself The accessor-declarations, which must be enclosed in "{" and "}" tokens, declare the accessors of the indexer. The accessors specify the executable statements associated with reading and writing indexer elements.
Even though the syntax for accessing an indexer element is the same as that for an array element, an indexer element is not classified as a variable. Thus, it is not possible to pass an indexer element as a ref or out parameter.
The formal parameter list of an indexer defines the signature of the indexer. Specifically, the signature of an indexer consists of the number and types of its formal parameters. The element type is not part of an indexer's signature, nor are the names of the formal parameters. The signature of an indexer must differ from the signatures of all other indexers declared in the same class.
Indexers and properties are very similar in concept, but differ in the following ways,
Join the conversation! Your thoughts help the community grow.