Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml


Indexers

C sharp



+ Font mai mare | - Font mai mic



Indexers

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:



indexer-declaration:
attributesopt indexer-modifiersopt indexer-declarator

indexer-modifiers:
indexer-modifier
indexer-modifiers indexer-modifier

indexer-modifier:
new
public
protected
internal
private
virtual
sealed
override
abstract

indexer-declarator:
type
this [ formal-parameter-list ]
type interface-type
. this [ formal-parameter-list ]

An indexer-declaration may include a set of attributes (17), a new modifier (10.2.2), a valid combination of the four access modifiers (10.2.3), and a valid combination of the virtual (10.5.3), override (10.5.4), and abstract (10.5.6) modifiers. In addition, an indexer that includes the override modifier may also include the sealed modifier (10.5.5).

The virtual, override, and abstract modifiers are mutually exclusive except in one case. The abstract and override modifiers may be used together so that an abstract indexer can override a virtual one.

The type of an indexer declaration specifies the element type of the indexer introduced by the declaration. Unless the indexer is an explicit interface member implementation, the type is followed by the keyword this. For an explicit interface member implementation, the type is followed by an interface-type, a ".", and the keyword this. 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 indexer corresponds to that of a method (10.5.1), 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 (3.5.4).

The accessor-declarations (10.6.2) which must be enclosed in "" 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 argument.

The formal parameter list of an indexer defines the signature (3.6) of the indexer. Specifically, the signature of an indexer consists of the number and types of its formal parameters. The element type and names of the formal parameters 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 (7.5.2) or a member-access (7.5.4), whereas an indexer element is accessed through an element-access (7.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 an indexer 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 set accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus an additional parameter named value.

It is an error for an indexer accessor to declare a local variable with the same name as an indexer parameter.

In an overriding property declaration, the inherited property is accessed using the syntax base.P, where P is the property name. In an overriding indexer declaration, the inherited indexer is accessed using the syntax base[E], where E is a comma separated list of expressions.

With these differences in mind, all rules defined in 10.6.2 and 10.6.3 apply to indexer accessors as well as property accessors.

The example below declares a BitArray class that implements an indexer for accessing the individual bits in the bit array.

class BitArray

public int Length
}

public bool this[int index]
return (bits[index >> 5] & 1 << index) != 0;
}
set
if (value)
else
}
}
}

An instance of the BitArray class consumes substantially less memory than a corresponding bool[] (since each value of the former occupies only one bit instead of the latter's one byte), but it permits the same operations as a bool[].

The following CountPrimes class uses a BitArray and the classical "sieve" algorithm to compute the number of primes between 1 and a given maximum:

class CountPrimes

}
return count;
}

static void Main(string[] args) primes between 1 and ', count, max);
}
}

Note that the syntax for accessing elements of the BitArray is precisely the same as for a bool[].

The following example shows a 26 by 10 grid class that has an indexer with two parameters. The first parameter is required to be an upper- or lowercase letter in the range A-Z, and the second is required to be an integer in the range 0-9.

class Grid

set
}
}

Indexer overloading

The indexer overload resolution rules are described in 7.4.2.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 919
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved