logo
CSharp_Prog_Guide

Результат

Element #0 = 0

Element #1 = 0

Element #2 = 4

Element #3 = 0

Element #4 = 0

Element #5 = 32

Element #6 = 0

Element #7 = 0

Element #8 = 0

Element #9 = 0

Element #10 = 0

В предыдущем пример можно использовать явную реализацию члена интерфейса с помощью полного имени члена интерфейса. Пример.

public string ISomeInterface.this

{

}

Однако, полное имя требуется для исключения неоднозначности только тогда, когда класс реализует более одного интерфейса с одинаковой сигнатурой индексатора. Например, если класс Employee реализует два интерфейса ICitizen и IEmployee с одинаковой подписью индексатора, требуется явная реализация члена интерфейса. Поэтому следующее объявление индексатора:

public string IEmployee.this

{

}

реализует индексатор для интерфейса IEmployee, а следующее объявление:

public string ICitizen.this

{

}

реализует индексатор для интерфейса ICitizen.

Comparison Between Properties and Indexers

Indexers are like properties. Except for the differences shown in the following table, all the rules that are defined for property accessors apply to indexer accessors also.

Property

Indexer

Allows methods to be called as if they were public data members.

Allows elements of an internal collection of an object to be accessed by using array notation on the object itself.

Accessed through a simple name.

Accessed through an index.

Can be a static or an instance member.

Must be an instance member.

A get accessor of a property has no parameters.

A get accessor of an indexer has the same formal parameter list as the indexer.

A set accessor of a property contains the implicit value parameter.

A set accessor of an indexer has the same formal parameter list as the indexer, and also to the value parameter.

Supports shortened syntax with Auto-Implemented Properties.

Does not support shortened syntax.