Indexer -indexer (MSDN)

xiaoxiao2021-03-06  85

The indexer allows an instance of the class or structure to index in the same manner as the array. Indexes are similar to attributes, different is their accessor using parameters. (Establish an index for instances of the class or structure) The indexer enables you to establish an index in the same way as the array. To declare the indexer, use the following: [Attributes] [Modifiers] Indexer-Declarator {Accessor-Declarations} Indexer-Declarator adopts one of the following forms: Type this [formal-index-parameter-list]

Type interface-type.this [formal-index-parameter-list] formal-index-parameter is: [Attributes] Type Identifier:

Attributes (optional)

Additional declarative information. For more information on properties and attribute classes, see

C # attribute

.

Modifiers (optional)

The modifier that can be used is New, Virtual, SeaRed, Override, Abstract, Extern, and four access modifiers. For more information, see

Access modifier

.

INDEXER-DECLARATOR

Includes element type (ie) introduced by the inderator and formal-index-parameter-list. If the indexer is implemented as an explicit interface member, it includes Interface-Type.

Type

type name.

Interface-Type

Interface name.

FORMAL-INDEX-Parameter-List

Specify the parameters of the indexer. The parameters include optional attributes, index type, and index identifier. You must specify at least one parameter. Parameter modifiers OUT and REF are not allowed.

Accessor-declarations

Indexer Accessor, which specifies executable statements related to the read and write indexer elements.

Identifier

parameter name.

The Get Access Device of the GET Accessor Inderator is similar to the method body. It returns the type of indexer. The GET Accessor uses the same FORMAL-INDEX-Parameter-List as the indexer. For example: get

{

Return myarray [index];

} The SET access device of the SET Accessor Inderator is similar to the method body. In addition to the Value implicit parameters, it also uses the same FORMAL-INDEX-Parameter-List as the indexer. SET

{

MyArray [INDEX] = VALUE;

} The type of note indexer and each type referenced in the FORMAL-INDEX-Parameter-List must be at least as accessible as the indexer itself. For more information on accessible levels, see Accessing the modifier. The signature of the indexer consists of the quantity and type of its shape. It does not include an indexer type or shape reference. If more than one indexer is declared in the same category, they must have different signatures. The indexer value is not classified as a variable; therefore, it is impossible to pass the indexer value as the REF or OUT parameter. To provide a name that can be used by other languages ​​for the default index properties by other languages, the Name property can be used in the declaration. For example: [System.Runtime.CompilerServices.csharp.indexerName ("MyItem")]

Public int this [int index] // Indexer Declaration

{

} This index will have a name MyItem. If the NAME property is not provided, the default name is Item. Example The following example shows how to declare a private array field, MyArray, and an indexer. Direct access to instance B [I] by using an indexer. Another way to use the indexer is to declare the array as a member of the PUBLIC member and directly access its member MyArray [i]. // cs_keyword_indexers.cs concernsing system;

Class IndexerClass

{

Private int [] myarray = new int [100];

Public int this [int index] // Indexer Declaration

{

get

{

// Check the index limited.

IF (INDEX <0 || index> = 100)

Return 0;

Else

Return myarray [index];

}

set

{

IF (! (中 <0 || index> = 100))

MyArray [INDEX] = VALUE;

}

}

}

Public class mainclass

{

Public static void main ()

{

IndexerClass B = New IndexerClass ();

// Call the indexer to initialize the elements # 3 and # 5.

B [3] = 256;

B [5] = 1024;

For (int i = 0; i <= 10; i )

{

Console.writeline ("Element # {0} = {1}", i, b [i]);

}

}

} Output ELEMENT # 0 = 0

ELEMENT # 1 = 0

ELEMENT # 2 = 0

ELEMENT # 3 = 256

ELEMENT # 4 = 0

ELEMENT # 5 = 1024

ELEMENT # 6 = 0

ELEMENT # 7 = 0

ELEMENT # 8 = 0

ELEMENT # 9 = 0

ELEMENT # 10 = 0

Note that when calculating the access of the indexer (for example, in the Console.write statement), the GET Accessor is called. Therefore, if the Get Accessor does not exist, the compile time error will occur.

When the indexer declares contains the extern modifier, the indexer is called an external indexer. Because the external indexer declares does not provide any actual implementation, each of its accessor declarations consists of a semicolon. The following example declares a BitArray class that implements an indexer to access a single bit in the bit array. Using system;

Class BitArray

{

Int [] bits;

Int length;

Public BitArray (int LENGTH) {

IF (Length <0) throw new argumentexception ();

Bits = new int in ((Length - 1) >> 5) 1];

THIS.LENGTH = Length;

}

Public int lay {

Get {return longth;}

}

Public bool this [int index] {

Get {

IF (INDEX <0 || index> = length) {

Throw new indexoutofrangeexception ();

}

Return (Bits [Index >> 5] & 1 << index)! = 0;

}

SET {

IF (INDEX <0 || index> = length) {

Throw new indexoutofrangeexception ();

}

IF (value) {

Bits [INDEX >> 5] | = 1 << index;

}

Else {

Bits [INDEX >> 5] & = ~ (1 << index);

}

}

}

} The memory of the BitArray class is far less than the corresponding BOOL [] (this is because each value of the former accounts for only one, and each value of the latter is one byte), and it can perform BOOL [] The same operation. The following CountPrimes class uses BitArray and classic "filter" algorithms to calculate 1 and given maximum number of prime numbers: Class CountPrimes

{

Static Int Count (int max) {

BitArray Flags = New BitArray (MAX 1);

INT count = 1;

For (int i = 2; i <= max; i ) {

IF (! Flags [i]) {

For (int J = i * 2; j <= max; j = i) Flags [J] = true;

COUNT ;

}

}

Return count;

}

Static void main (string [] args) {

INT max = int.parse (args [0]);

INT count = count (max);

Console.writeline ("Found {0} Primes Between 1 and {1}", count, max);

}

} Please note that the syntax of the elements that access BitArtArray is identical to the syntax of the elements of accessing BOOL []. The following example shows a 26 multiplier class with an indexer with two parameters. The first parameter must be uppercase or lowercase letters within the A-Z range, and the second parameter must be an integer in the range of 0-9.

Using system;

Class Grid

{

Const int NumRows = 26;

Const int numcols = 10;

int [,] cells = new int [numrows, numcolls];

Public int this [char c, int co1 {

Get {

C = char.toupper (c);

IF (c <'a' || c> 'z') {

Throw new argumentexception ();

}

IF (col <0 || col> = numcols) {

Throw new indexoutofrangeexception ();

}

Return Cells [C - 'A', Col];

}

SET {

C = char.toupper (c);

IF (c <'a' || c> 'z') {

Throw new argumentexception ();

}

IF (col <0 || col> = numcols) {

Throw new indexoutofrangeexception ();

}

Cells [C - 'A', Col] = Value;

}

}

转载请注明原文地址:https://www.9cbs.com/read-96646.html

New Post(0)