Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 26 Generics

126.3 Generic interface declarations

2[Note: An interface declaration (§20.1), can optionally define type parameters and their associated

3constraints:

4interface-declaration:

5

attributesopt interface-modifiersopt partialopt interface identifier type-parameter-listopt

6

interface-baseopt type-parameter-constraints-clausesopt interface-body ;opt

7end note]

8Except where noted below, generic interface declarations follow the same rules as non-generic interface

9declarations.

10Each type parameter in an interface declaration defines a name in the declaration space (§10.3) of that

11interface. The scope (§10.7) of a type parameter on an interface includes the interface-base, type-parameter-

12constraints-clauses, and interface-body. Within its scope, a type parameter can be used as a type. The same

13restrictions apply to type parameters on interfaces as apply to type parameter on classes (§26.1.1).

1426.3.1 Uniqueness of implemented interfaces

15The interfaces implemented by a generic type declaration shall remain unique for all possible constructed

16types. Without this rule, it would be impossible to determine the correct method to call for certain

17constructed types. [Example: Suppose a generic class declaration were permitted to be written as follows:

18interface I<T>

19{

20void F();

21}

22

class X<U, V>: I<U>, I<V>

// Error: I<U> and I<V> conflict

23

{

 

24

void I<U>.F() {…}

 

25void I<V>.F() {…}

26}

27Were this permitted, it would be impossible to determine which code to execute in the following case:

28I<int> x = new X<int,int>();

29x.F();

30end example]

31To determine if the interface list of a generic type declaration is valid, the following steps are performed:

32Let L be the list of interfaces directly specified in a generic class, struct, or interface declaration C.

33Add to L any base interfaces of the interfaces already in L.

34Remove any duplicates from L.

35If any possible constructed type created from C would, after type arguments are substituted into L, cause

36two interfaces in L to be identical, then the declaration of C is invalid. Constraint declarations are not

37considered when determining all possible constructed types.

38[Note: In the class declaration X above, the interface list L consists of I<U> and I<V>. The declaration is

39invalid because any constructed type with U and V being the same type would cause these two interfaces to

40be identical types. end note]

41[Example: It is possible for interfaces specified at different inheritance levels to unify:

42interface I<T>

43{

44void F();

45}

405

Соседние файлы в предмете Электротехника