Search C # (index one) a week

zhaozj2021-02-16  45

Search C # (index one) a week

C # Talent Bird (QQ: 249178521)

1.[]

· Index provides the syntax of [] class

W is always a member of an instance, it can be virtual

W No ref / OUT parameters

Struct stringsection

{

...

Public char this [int At]

{

...

}

...

}

StringSECTION CORD ("Csharp", 2, 6);

...

Char first = Cord [0]; // 'h'

...

Console.writeline (Cord); // Harp

The index declaration includes: an optional access modifier (example is a public), the type of return value (char), keyword this (can not be omitted), and a parameter similar to function parameters (not square brackets, not functions Parentheses), then the index. You can't use a static index, so you can't use Static keywords in the indexed statement.

Index can be declared as virtual, so it can be overloaded in its derived class.

The parameters of the index cannot use the REF / OUT type parameters. E.g:

Struct stringsection

{

Public char this [ref int at] / / error

...

}

2. Read and write

· Index can only contain the following statement

W get {} is used to read

W set {}

Struct stringsection

{

...

Public char excitation [int at] {

Get {return ...;

Set {... = value;

}

...

}

Cord [3] = 'E';

IF (CORD [3] == 'E') ...

The index declaration and properties are the same: can only contain the set / get statement.

When using an index expression for read operation, the indexed GET statement is automatically run, for example, in the above example, the expression:

Cord [3] == 'e'

The system will automatically call the indexed GET statement (3 to the index of the index), return a CHAR type value, which is then compared with 'E'.

When using an index expression for writing, the indexed SET statement is automatically run, for example, in the above example, the expression:

Cord [3] = 'e'

The system will automatically call the indexed SET statement (3 pass to the index ", the SET statement can get the value on the right side of the expression, which is obtained through the" keyword "of Value.

3. Read only or write

· Only the index of the get statement is

W read-only index

· Only the index of the SET statement is

w write index only

Struct stringsection

{

...

Public char this [int At]

{

Get {return ...}

}

...

}

StringSECTION CORD ("Csharp", 1, 6);

...

IF (Cord [4] == 'k') {

Cord [4] = 'k'; // Error

}

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

New Post(0)