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

C# LANGUAGE SPECIFICATION

114.10.1 Integer logical operators

2The predefined integer logical operators are:

3int operator &(int x, int y);

4uint operator &(uint x, uint y);

5long operator &(long x, long y);

6ulong operator &(ulong x, ulong y);

7void operator &(long x, ulong y);

8void operator &(ulong x, long y);

9int operator |(int x, int y);

10uint operator |(uint x, uint y);

11long operator |(long x, long y);

12ulong operator |(ulong x, ulong y);

13void operator |(long x, ulong y);

14void operator |(ulong x, long y);

15int operator ^(int x, int y);

16uint operator ^(uint x, uint y);

17long operator ^(long x, long y);

18ulong operator ^(ulong x, ulong y);

19void operator ^(long x, ulong y);

20void operator ^(ulong x, long y);

21The & operator computes the bitwise logical AND of the two operands, the | operator computes the bitwise

22logical OR of the two operands, and the ^ operator computes the bitwise logical exclusive OR of the two

23operands. No overflows are possible from these operations. The operators with void return type always

24produce a compile-time error. Consequently, it is an error for one operand to be of type long and the other

25to be of type ulong.

2614.10.2 Enumeration logical operators

27Every enumeration type E implicitly provides the following predefined logical operators:

28E operator &(E x, E y);

29E operator |(E x, E y);

30E operator ^(E x, E y);

31The result of evaluating x op y, where x and y are expressions of an enumeration type E with an underlying

32type U, and op is one of the logical operators, is exactly the same as evaluating (E)((U)x op (U)y). In other

33words, the enumeration type logical operators simply perform the logical operation on the underlying type of

34the two operands. These operators are only considered by overload resolution (§14.2.4) when one of the

35actual operands is of type E.

3614.10.3 Boolean logical operators

37The predefined boolean logical operators are:

38bool operator &(bool x, bool y);

39bool operator |(bool x, bool y);

40bool operator ^(bool x, bool y);

41The result of x & y is true if both x and y are true. Otherwise, the result is false.

42The result of x | y is true if either x or y is true. Otherwise, the result is false.

43The result of x ^ y is true if x is true and y is false, or x is false and y is true. Otherwise, the result

44is false. When the operands are of type bool, the ^ operator computes the same result as the != operator.

4514.11 Conditional logical operators

46The && and || operators are called the conditional logical operators. They are also called the “short-

47circuiting” logical operators.

48conditional-and-expression:

49

inclusive-or-expression

50

conditional-and-expression && inclusive-or-expression

206

 

 

Chapter 14 Expressions

1

 

conditional-or-expression:

2

 

conditional-and-expression

3

 

conditional-or-expression || conditional-and-expression

4

The && and || operators are conditional versions of the & and | operators:

5

The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is true.

6

The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is

7

 

false.

8

An operation of the form x && y or x || y is processed by applying overload resolution (§14.2.4) as if the

9operation was written x & y or x | y. Then,

10If overload resolution fails to find a single best operator, or if overload resolution selects one of the

11predefined integer logical operators, a compile-time error occurs.

12Otherwise, if the selected operator is one of the predefined boolean logical operators (§14.10.2), the

13operation is processed as described in §14.11.1.

14Otherwise, the selected operator is a user-defined operator, and the operation is processed as described

15in §14.11.2.

16It is not possible to directly overload the conditional logical operators. However, because the conditional

17logical operators are evaluated in terms of the regular logical operators, overloads of the regular logical

18operators are, with certain restrictions, also considered overloads of the conditional logical operators. This is

19described further in §14.11.2.

2014.11.1 Boolean conditional logical operators

21

When overload resolution (§14.2.4) for x & y or x | y selects the predefined boolean & or | operator, then

22

x && y or x || y is processed as follows:

23

The operation x && y is evaluated as (bool)x ? (bool)y : false. In other words, x is first

24evaluated and converted to type bool. Then, if x is true, y is evaluated and converted to type bool,

25and this becomes the result of the operation. Otherwise, the result of the operation is false.

26 The operation x || y is evaluated as (bool)x ? true : (bool)y. In other words, x is first

27evaluated and converted to type bool. Then, if x is true, the result of the operation is true. Otherwise,

28y is evaluated and converted to type bool, and this becomes the result of the operation.

2914.11.2 User-defined conditional logical operators

30

When overload resolution (§14.2.4) for x & y or x | y selects a user defined & or | operator, then

31

processing x && y or x || y requires that both of the following shall be true, where T is the type in which

32the selected operator is declared:

33The return type and the type of each parameter of the selected operator shall be T. In other words, the

34operator shall compute the logical AND or the logical OR of two operands of type T, and shall return a

35result of type T.

36T shall contain declarations of operator true and operator false.

37A compile-time error occurs if either of these requirements is not satisfied. Otherwise, the && or ||

38operation is evaluated by combining the user-defined operator true or operator false with the

39selected user-defined operator:

40 The operation x && y is evaluated as T.false((T)x) ? (T)x : T.&((T)x, y), where

41T.false((T)x) is an invocation of the operator false declared in T, and T.&((T)x, y) is an

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

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

44to determine if x is definitely false. Then, if x is definitely false, the result of the operation is the value

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

207

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