13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> LANGUAGE SPECIFICATIONThe type of an indexer declaration specifies the element type of the indexer introduced by the declaration. Unlessthe indexer is an explicit interface member implementation, the type is followed by the keyword this. For anexplicit interface member implementation, the type is followed by an interface-type, a “.”, and the keywordthis. Unlike other members, indexers do not have user-defined names.The formal-parameter-list specifies the parameters of the indexer. The formal parameter list of an indexercorresponds to that of a method (§17.5.1), except that at least one parameter must be specified, and that the refand 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 accessibleas the indexer itself (§10.5.4).The accessor-declarations (§17.6.2), which must be enclosed in “{” and “}” tokens, declare the accessors of theindexer. 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 indexerelement is not classified as a variable. Thus, it is not possible to pass an indexer element as a ref or outargument.The formal-parameter-list of an indexer defines the signature (§10.6) of the indexer. Specifically, the signature ofan indexer consists of the number and types of its formal parameters. The element type and names of the formalparameters are not part of an indexer’s signature.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:• A property is identified by its name, whereas an indexer is identified by its signature.• A property is accessed through a simple-name (§14.5.2) or a member-access (§14.5.4), whereas an indexerelement is accessed through an element-access (§14.5.6.2).• A property can be a static member, whereas an indexer is always an instance member.• A get accessor of a property corresponds to a method with no parameters, whereas a get accessor of anindexer corresponds to a method with the same formal parameter list as the indexer.• A set accessor of a property corresponds to a method with a single parameter named value, whereas a setaccessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus anadditional parameter named value.• It is a compile-time error for an indexer accessor to declare a local variable with the same name as an indexerparameter.• In an overriding property declaration, the inherited property is accessed using the syntax base.P, where P isthe property name. In an overriding indexer declaration, the inherited indexer is accessed using the syntaxbase[E], where E is a comma-separated list of expressions.Aside from these differences, all rules defined in §17.6.2 and §17.6.3 apply to indexer accessors as well as toproperty accessors.When an indexer declaration includes an extern modifier, the indexer is said to be an external indexer. Becausean external indexer declaration provides no actual implementation, each of its accessor-declarations consists of asemicolon.[Example: The example below declares a BitArray class that implements an indexer for accessing theindividual bits in the bit array.using System;class BitArray{int[] bits;int length;252

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!