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

Chapter 14 Expressions

1Otherwise, if an explicit reference conversion (§13.2.3) exists from the compile-time type of e to T, a

2dynamic type check is performed:

3o If the value of e is null, the result is the value null with the compile-time type T.

4o Otherwise, let R be the run-time type of the instance referenced by e. If R and T are the same type, if

5R is a reference type and an implicit reference conversion from R to T exists, or if R is a value type

6and T is an interface type that is implemented by R, the result is the reference given by e with the

7compile-time type T.

8o Otherwise, the result is the value null with the compile-time type T.

9Otherwise, if the compile-time type of e or T is an open type (§26.5.2), a dynamic type check is

10performed.

11Otherwise, the indicated conversion is never possible, and a compile-time error occurs.

12The as operator only performs reference conversions and boxing conversions. Other conversions, such as

13user defined conversions, are not possible with the as operator and should instead be performed using cast

14expressions.

15The as operator can be used with a type parameter T (§26.1.1) as the right-hand side only if T is known to

16be a reference type (§26.7). This restriction is required because the value null might be returned as a result

17of the operator. [Example:

18class X

19{

20

public T F<T>(object o) where T: Attribute {

21

return o as T;

// Ok, T has a class constraint

22

}

 

23

public T G<T>(object o) {

 

24

return o as T;

// Error, unconstrained T

25}

26}

27end example]

28[Note: When constraints are factored in, there are situations where the compiler can conclude that the

29operation e as T will never succeed, for example when e is a struct or sealed class that doesn’t implement

30a particular interface, and T is constrained to require that interface. However, a complete specification of

31those situations is complicated, and the rules given above seem to be the best compromise. end note]

3214.10 Logical operators

33The &, ^, and | operators are called the logical operators.

34and-expression:

35

equality-expression

36

and-expression & equality-expression

37

exclusive-or-expression:

38

and-expression

39

exclusive-or-expression ^ and-expression

40

inclusive-or-expression:

41

exclusive-or-expression

42

inclusive-or-expression | exclusive-or-expression

43For an operation of the form x op y, where op is one of the logical operators, overload resolution (§14.2.4) is

44applied to select a specific operator implementation. The operands are converted to the parameter types of

45the selected operator, and the type of the result is the return type of the operator.

46The predefined logical operators are described in the following subclauses.

205

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