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

C# LANGUAGE SPECIFICATION

1However, for type object, pre-defined conversions hide the user-defined conversions in all cases but one:

2void F(object o, Convertible<object> n) {

3

o = n;

// Pre-defined boxing conversion

4

o = (object)n;

// Pre-defined boxing conversion

5

n = o;

// User-defined implicit conversion

6

n = (Convertible<object>)o;

// Pre-defined unboxing conversion

7}

8end example]

926.1.11 Nested types in generic classes

10A generic class declaration can contain nested type declarations. The type parameters of the enclosing class

11can be used within the nested types. A nested type declaration can contain additional type parameters that

12apply only to the nested type.

13Every type declaration contained within a generic class declaration is implicitly a generic type declaration.

14When writing a reference to a type nested within a generic type, the containing constructed type, including

15its type arguments, shall be named. However, from within the outer class, the nested type can be used

16without qualification; the instance type of the outer class can be implicitly used when constructing the nested

17type. [Example: The following example shows three different correct ways to refer to a constructed type

18created from Inner; the first two are equivalent:

19class Outer<T>

20{

21

class Inner<U>

 

 

22

{

 

 

23

public static void F(T t, U u) {…}

 

 

24

}

 

 

25

static void F(T t) {

 

 

26

Outer<T>.Inner<string>.F(t, "abc");

 

// These two statements have

27

Inner<string>.F(t, "abc");

 

// the same effect

28

Outer<int>.Inner<string>.F(3, "abc");

// This type is different

29

Outer.Inner<string>.F(t, "abc");

// Error, Outer needs type arg

30}

31}

32end example]

33Although it is bad programming style, a type parameter in a nested type can hide a member or type

34parameter declared in the outer type. [Example:

35class Outer<T>

36{

37

class Inner<T>

// Valid, hides Outer’s T

38

{

 

39

public T t;

// Refers to Inner’s T

40}

41}

42end example]

4326.2 Generic struct declarations

44[Note: A struct declaration (§18.1), can optionally define type parameters and their associated constraints:

45struct-declaration:

46

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

47

struct-interfacesopt type-parameter-constraints-clausesopt struct-body ;opt

48end note]

49The rules for generic class declarations (§26.1 and its subclauses) apply equally to generic struct

50declarations, as do the exceptions noted in §18.3.

404

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