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

C# LANGUAGE SPECIFICATION

1is invoked on the value previously computed for x converted to type T and the value computed for y to

2produce the result of the operation.

3 The operation x || y is evaluated as T.true((T)x) ? (T)x : T.|((T)x, y), where

4T.true((T)x) is an invocation of the operator true declared in T, and T.|((T)x, y) is an

5invocation of the selected operator |. In addition, the value (T)x shall only be evaluated once. In

6other words, x is first evaluated and converted to type T and operator true is invoked on the result to

7determine if x is definitely true. Then, if x is definitely true, the result of the operation is the value

8previously computed for x converted to type T. Otherwise, y is evaluated, and the selected operator |

9is invoked on the value previously computed for x converted to type T and the value computed for y to

10produce the result of the operation.

11In either of these operations, the expression given by x is only evaluated once, and the expression given by y

12is either not evaluated or evaluated exactly once.

13For an example of a type that implements operator true and operator false, see §18.4.2.

1414.12 Conditional operator

15The ?: operator is called the conditional operator. It is at times also called the ternary operator.

16conditional-expression:

17

conditional-or-expression

 

18

conditional-or-expression

? expression : expression

19A conditional expression of the form b ? x : y first evaluates the condition b. Then, if b is true, x is

20evaluated and becomes the result of the operation. Otherwise, y is evaluated and becomes the result of the

21operation. A conditional expression never evaluates both x and y.

22The conditional operator is right-associative, meaning that operations are grouped from right to left.

23 [Example: An expression of the form a ? b : c ? d : e is evaluated as a ? b : (c ? d : e). end

24example]

25The first operand of the ?: operator shall be an expression of a type that can be implicitly converted to

26bool, or an expression of a type that implements operator true. If neither of these requirements is

27satisfied, a compile-time error occurs.

28The second and third operands of the ?: operator control the type of the conditional expression. Let X and Y

29be the types of the second and third operands. Then,

30If X and Y are the same type, then this is the type of the conditional expression.

31Otherwise, if an implicit conversion (§13.1) exists from X to Y, but not from Y to X, then Y is the type of

32the conditional expression.

33Otherwise, if an implicit conversion (§13.1) exists from Y to X, but not from X to Y, then X is the type of

34the conditional expression.

35Otherwise, no expression type can be determined, and a compile-time error occurs.

36The run-time processing of a conditional expression of the form b ? x : y consists of the following steps:

37First, b is evaluated, and the bool value of b is determined:

38o If an implicit conversion from the type of b to bool exists, then this implicit conversion is

39performed to produce a bool value.

40o Otherwise, the operator true defined by the type of b is invoked to produce a bool value.

41If the bool value produced by the step above is true, then x is evaluated and converted to the type of

42the conditional expression, and this becomes the result of the conditional expression.

43Otherwise, y is evaluated and converted to the type of the conditional expression, and this becomes the

44result of the conditional expression.

208

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