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

C# LANGUAGE SPECIFICATION

1When x is of type int or long, the low-order bits of x are discarded, the remaining bits are shifted

2right, and the high-order empty bit positions are set to zero if x is non-negative and set to one if x is

3negative.

4When x is of type uint or ulong, the low-order bits of x are discarded, the remaining bits are shifted

5right, and the high-order empty bit positions are set to zero.

6For the predefined operators, the number of bits to shift is computed as follows:

7When the type of x is int or uint, the shift count is given by the low-order five bits of count. In other

8 words, the shift count is computed from count & 0x1F.

9When the type of x is long or ulong, the shift count is given by the low-order six bits of count. In

10other words, the shift count is computed from count & 0x3F.

11If the resulting shift count is zero, the shift operators simply return the value of x.

12Shift operations never cause overflows and produce the same results in checked and unchecked contexts.

13When the left operand of the >> operator is of a signed integral type, the operator performs an arithmetic

14shift right wherein the value of the most significant bit (the sign bit) of the operand is propagated to the

15high-order empty bit positions. When the left operand of the >> operator is of an unsigned integral type, the

16operator performs a logical shift right wherein high-order empty bit positions are always set to zero. To

17perform the opposite operation of that inferred from the operand type, explicit casts can be used. [Example:

18If x is a variable of type int, the operation unchecked((int)((uint)x >> y)) performs a logical shift

19right of x. end example]

2014.9 Relational and type-testing operators

21The ==, !=, <, >, <=, >=, is and as operators are called the relational and type-testing operators.

22relational-expression:

23

shift-expression

24

relational-expression < shift-expression

25

relational-expression > shift-expression

26

relational-expression <= shift-expression

27

relational-expression >= shift-expression

28

relational-expression is type

29

relational-expression as type

30

equality-expression:

31

relational-expression

32

equality-expression == relational-expression

33

equality-expression != relational-expression

34The is operator is described in §14.9.9 and the as operator is described in §14.9.10.

35The ==, !=, <, >, <= and >= operators are comparison operators. For an operation of the form x op y, where

36op is a comparison operator, overload resolution (§14.2.4) is applied to select a specific operator

37implementation. The operands are converted to the parameter types of the selected operator, and the type of

38the result is the return type of the operator. If both operands of an equality-expression have the null type

39(and hence the null value as well), then overload resolution is not performed and the expression evaluates

40to a contant value of true or false according to whether the operator is == or !=.

41The predefined comparison operators are described in the following subclauses. All predefined comparison

42operators return a result of type bool, as described in the following table.

43

198

Chapter 14 Expressions

Operation

Result

 

 

x == y

true if x is equal to y, false otherwise

 

 

x != y

true if x is not equal to y, false otherwise

 

 

x < y

true if x is less than y, false otherwise

 

 

x > y

true if x is greater than y, false otherwise

 

 

x <= y

true if x is less than or equal to y, false otherwise

 

 

x >= y

true if x is greater than or equal to y, false otherwise

 

 

1

214.9.1 Integer comparison operators

3The predefined integer comparison operators are:

4bool operator ==(int x, int y);

5bool operator ==(uint x, uint y);

6bool operator ==(long x, long y);

7bool operator ==(ulong x, ulong y);

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

9void operator ==(ulong x, long y);

10bool operator !=(int x, int y);

11bool operator !=(uint x, uint y);

12bool operator !=(long x, long y);

13bool operator !=(ulong x, ulong y);

14void operator !=(long x, ulong y);

15void operator !=(ulong x, long y);

16bool operator <(int x, int y);

17bool operator <(uint x, uint y);

18bool operator <(long x, long y);

19bool operator <(ulong x, ulong y);

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

21void operator >(ulong x, long y);

22bool operator >(int x, int y);

23bool operator >(uint x, uint y);

24bool operator >(long x, long y);

25bool operator >(ulong x, ulong y);

26void operator >(long x, ulong y);

27void operator >(ulong x, long y);

28bool operator <=(int x, int y);

29bool operator <=(uint x, uint y);

30bool operator <=(long x, long y);

31bool operator <=(ulong x, ulong y);

32void operator <=(long x, ulong y);

33void operator <=(ulong x, long y);

34bool operator >=(int x, int y);

35bool operator >=(uint x, uint y);

36bool operator >=(long x, long y);

37bool operator >=(ulong x, ulong y);

38void operator >=(long x, ulong y);

39void operator >=(ulong x, long y);

40Each of these operators compares the numeric values of the two integer operands and returns a bool

41value that indicates whether the particular relation is true or false. The operators with void return

42type always produce a compile-time error. Consequently, it is an error for one operand to be of type

43long and the other to be of type ulong.

4414.9.2 Floating-point comparison operators

45The predefined floating-point comparison operators are:

199

C# LANGUAGE SPECIFICATION

1bool operator ==(float x, float y);

2bool operator ==(double x, double y);

3bool operator !=(float x, float y);

4bool operator !=(double x, double y);

5bool operator <(float x, float y);

6bool operator <(double x, double y);

7bool operator >(float x, float y);

8bool operator >(double x, double y);

9bool operator <=(float x, float y);

10bool operator <=(double x, double y);

11bool operator >=(float x, float y);

12bool operator >=(double x, double y);

13The operators compare the operands according to the rules of the IEC 60559 standard:

14If either operand is NaN, the result is false for all operators except !=, for which the result is true.

15For any two operands, x != y always produces the same result as !(x == y). However, when one or

16both operands are NaN, the <, >, <=, and >= operators do not produce the same results as the logical

17negation of the opposite operator. [Example: If either of x and y is NaN, then x < y is false, but

18!(x >= y) is true. end example]

19When neither operand is NaN, the operators compare the values of the two floating-point operands with

20respect to the ordering

21–∞ < –max < … < –min < –0.0 == +0.0 < +min < … < +max < +∞

22where min and max are the smallest and largest positive finite values that can be represented in the given

23floating-point format. Notable effects of this ordering are:

24o Negative and positive zeros are considered equal.

25o A negative infinity is considered less than all other values, but equal to another negative infinity.

26o A positive infinity is considered greater than all other values, but equal to another positive infinity.

2714.9.3 Decimal comparison operators

28The predefined decimal comparison operators are:

29bool operator ==(decimal x, decimal y);

30bool operator !=(decimal x, decimal y);

31bool operator <(decimal x, decimal y);

32bool operator >(decimal x, decimal y);

33bool operator <=(decimal x, decimal y);

34bool operator >=(decimal x, decimal y);

35Each of these operators compares the numeric values of the two decimal operands and returns a bool value

36that indicates whether the particular relation is true or false. Each decimal comparison is equivalent to

37using the corresponding relational or equality operator of type System.Decimal.

3814.9.4 Boolean equality operators

39The predefined boolean equality operators are:

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

41bool operator !=(bool x, bool y);

42The result of == is true if both x and y are true or if both x and y are false. Otherwise, the result is

43false.

44The result of != is false if both x and y are true or if both x and y are false. Otherwise, the result is

45true. When the operands are of type bool, the != operator produces the same result as the ^ operator.

200

Chapter 14 Expressions

114.9.5 Enumeration comparison operators

2Every enumeration type implicitly provides the following predefined comparison operators:

3bool operator ==(E x, E y);

4bool operator !=(E x, E y);

5bool operator <(E x, E y);

6bool operator >(E x, E y);

7bool operator <=(E x, E y);

8bool operator >=(E x, E y);

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

10type U, and op is one of the comparison operators, is exactly the same as evaluating ((U)x) op ((U)y). In

11other words, the enumeration type comparison operators simply compare the underlying integral values of

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

13actual operands is of type E.

1414.9.6 Reference type equality operators

15Every class type C implicitly provides the following predefined reference type equality operators:

16bool operator ==(C x, C y);

17bool operator !=(C x, C y);

18unless predefined equality operators otherwise exists for C (for example, when C is string or

19System.Delegate).

20The operators return the result of comparing the two references for equality or non-equality.

21There are special rules for determining when a reference type equality operator is applicable. For an

22equality-expression with operands of type A and B, define A0 as follows:

23If A is a type parameter that does not have the value type constraint (§26.7), let A0 be the effective base

24class of A.

25Otherwise, if A is an interface type, a delegate type, an array type, a class type, or the null type, let A0

26be the same as A.

27Otherwise, no reference type equality operator is applicable.

28Now define A1 as follows:

29If A0 is an interface type, a delegate type, System.Delegate, or string, let A1 be object.

30Otherwise, if A0 is an array type, let A1 be System.Array.

31Otherwise, A0 is the null type or a class type and let A1 be the same as A0.

32Define B0 and B1 similarly. Now determine if any reference type equality operators are applicable as follows:

33If both of the types A and B are the null type, then overload resolution is not performed and the result is

34a constant true or false, as specified in §14.9.

35Otherwise, if A is the null type, then the reference type equality operator for B1 is applicable. If B is a

36type parameter and at run-time the type parameter is a value type, the result of the comparison is false.

37Otherwise, if B is the null type, then the reference type equality operator for A1 is applicable. If A is a

38type parameter and at run-time the type parameter is a value type, the result of the comparison is false.

39Otherwise, if either A or B is a type parameter that is not known to be a reference type (§26.7), then no

40reference type equality operator is applicable.

41Otherwise, if there is no identity or reference conversion (implicit or explicit) from A0 to B0 or no

42identity or reference conversion (implicit or explicit) from B0 to A0, then no reference type equality

43operator is applicable.

201

C# LANGUAGE SPECIFICATION

1Otherwise, if there is an identity or implicit reference conversion from A1 to B1, then the reference type

2equality operator for B1 is applicable.

3Otherwise, if there is an implicit reference conversion from B1 to A1, then the reference type equality

4operator for A1 is applicable.

5Otherwise no reference type equality operator is applicable.

6[Note: Notable implications of these rules are:

7The predefined reference type equality operators cannot be used to compare two references that are

8known to be different at compile-time. [Example: If the compile-time types of the operands are two class

9types A and B, and if neither A nor B derives from the other, then it would be impossible for the two

10operands to reference the same object and no reference type equality operator is applicable. Similarly, if

11A is a sealed class and B is an interface that A does not implement, then no reference type equality

12operator is applicable. end example]

13The predefined reference type equality operators do not permit value type operands to be compared.

14The predefined reference type equality operators never cause boxing operations to occur for their

15operands. It would be meaningless to perform such boxing operations, since references to the newly

16allocated boxed instances would necessarily differ from all other references.

17end note]

18When the operator overload resolution (§14.2.4) rules would pick an equality operator other than a reference

19type equality operator, selection of a reference type equality operator can be forced by explicitly casting one

20or both operands to type object. [Example: The example

21using System;

22class Test

23{

24

static void Main() {

25

string s = "Test";

26

string t = string.Copy(s);

27

Console.WriteLine(s == t);

28

Console.WriteLine((object)s == t);

29

Console.WriteLine(s == (object)t);

30

Console.WriteLine((object)s == (object)t);

31}

32}

33produces the output

34True

35False

36False

37False

38The s and t variables refer to two distinct string instances containing the same characters. The first

39comparison outputs True because the predefined string equality operator (§14.9.7) is selected when both

40operands are of type string. The remaining comparisons all output False because a predefined reference

41type equality operator is selected when one or both of the operands are of type object.

42Note that the above technique is not meaningful for value types. The example

43class Test

44{

45

static void Main() {

46

int i = 123;

47

int j = 123;

48

System.Console.WriteLine((object)i == (object)j);

49}

50}

51outputs False because the casts create references to two separate instances of boxed int values. end

52example]

202

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