What is indexer?

What is indexer?

Indexer:-Indexers permit instances of a class or struct to be indexed in the same way as arrays. Indexers are similar to properties except that their accessor take parameters. 

Indexers allow you to index a class or a struct instance in the same way as an array. To declare an indexer, use the following declaration:

[attributes] [modifiers] indexer-declarator {accessor-declarations}
The indexer-declarator takes one of the forms:

type this [formal-index-parameter-list]
type interface-type.this [formal-index-parameter-list]
The formal-index-parameter takes the form:

[attributes] type identifier

attributes (Optional) :-
Additional declarative information. For more information on attributes and attribute classes,
modifiers (Optional) :-
Allowed modifiers are new and a valid combination of the four access modifiers. 
indexer-declarator :-
Includes the type of the element introduced by the indexer, this, and the formal-index-parameter-list. If the indexer is an explicit interface member implementation, the interface-type is included. 
Specifies the parameters of the indexer. The parameter includes optional attributes, the index type, and the index identifier. At least one parameter must be specified. The parameters out and ref are not allowed. 
accessor-declarations :-
The indexer accessors, which specify the executable statements associated with reading and writing indexer elements. 

The get Accessor:-
The get accessor body of an indexer is similar to a method body. It returns the type of the indexer. The get accessor uses the same formal-index-parameter-list as the indexer.
 For example:

   return myArray[index];
The set Accessor:-
The set accessor body of an indexer is similar to a method body. It uses the same formal-index-parameter-list as the indexer, in addition to the value implicit parameter.
 For example:

   myArray[index] = value;

Date:2014-10-10 00:00:00

Post Your Answers

User Email:

User Name:

Related C Links

C interview questions and answers for experienced and fresher