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

Chapter 14 Expressions

1class Test

2{

3

void F() {

4

__Locals1 __locals1 = new __Locals1();

5

__locals1.__this = this;

6

__locals1.y = 123;

7

for (int i = 0; i < 10; i++) {

8

__Locals2 __locals2 = new __Locals2();

9

__locals2.__locals1 = __locals1;

10

__locals2.z = i * 2;

11

D d = new D(__locals2.__Method1);

12

}

13

}

14

class __Locals1

15

{

16

public Test __this;

17

public int y;

18

}

19

class __Locals2

20

{

21

public __Locals1 __locals1;

22

public int z;

23

public void __Method1() {

24

Console.WriteLine(__locals1.__this.x + __locals1.y + z);

25

}

26}

27}

28end note]

2914.6 Unary expressions

30unary-expression:

31

primary-expression

32

+

unary-expression

33

-

unary-expression

34

!

unary-expression

35

~

unary-expression

36

pre-increment-expression

37

pre-decrement-expression

38

cast-expression

3914.6.1 Unary plus operator

40For an operation of the form +x, unary operator overload resolution (§14.2.3) is applied to select a specific

41operator implementation. The operand is converted to the parameter type of the selected operator, and the

42type of the result is the return type of the operator. The predefined unary plus operators are:

43int operator +(int x);

44uint operator +(uint x);

45long operator +(long x);

46ulong operator +(ulong x);

47float operator +(float x);

48double operator +(double x);

49decimal operator +(decimal x);

50For each of these operators, the result is simply the value of the operand.

5114.6.2 Unary minus operator

52For an operation of the form –x, unary operator overload resolution (§14.2.3) is applied to select a specific

53operator implementation. The operand is converted to the parameter type of the selected operator, and the

54type of the result is the return type of the operator. The predefined negation operators are:

187

C# LANGUAGE SPECIFICATION

1Integer negation:

2int operator –(int x);

3long operator –(long x);

4The result is computed by subtracting x from zero. In a checked context, if the value of x is the

5smallest int or long (−231 or −263, respectively), a System.OverflowException is thrown. In an

6unchecked context, if the value of x is the smallest int or long, the result is that same value and the

7overflow is not reported.

8If the operand of the negation operator is of type uint, it is converted to type long, and the type of the

9result is long. An exception is the rule that permits the int value −2147483648 (−231) to be written as a

10decimal integer literal (§9.4.4.2).

11Negation of ulong is an error:

12void operator –(ulong x);

13Selection of this operator by unary operator overload resolution (§14.2.3) always results in a compile-

14time error. Consequently, if the operand of the negation operator is of type ulong, a compile-time error

15occurs. An exception is the rule that permits the long value −9223372036854775808 (−263) to be

16written as a decimal integer literal (§9.4.4.2).

17Floating-point negation:

18float operator –(float x);

19double operator –(double x);

20The result is the value of x with its sign inverted. If x is NaN, the result is also NaN.

21Decimal negation:

22decimal operator –(decimal x);

23The result is computed by subtracting x from zero.

24Decimal negation is equivalent to using the unary minus operator of type System.Decimal.

2514.6.3 Logical negation operator

26For an operation of the form !x, unary operator overload resolution (§14.2.3) is applied to select a specific

27operator implementation. The operand is converted to the parameter type of the selected operator, and the

28type of the result is the return type of the operator. Only one predefined logical negation operator exists:

29bool operator !(bool x);

30This operator computes the logical negation of the operand: If the operand is true, the result is false. If

31the operand is false, the result is true.

3214.6.4 Bitwise complement operator

33For an operation of the form ~x, unary operator overload resolution (§14.2.3) is applied to select a specific

34operator implementation. The operand is converted to the parameter type of the selected operator, and the

35type of the result is the return type of the operator. The predefined bitwise complement operators are:

36int operator ~(int x);

37uint operator ~(uint x);

38long operator ~(long x);

39ulong operator ~(ulong x);

40For each of these operators, the result of the operation is the bitwise complement of x.

41Every enumeration type E implicitly provides the following bitwise complement operator:

42E operator ~(E x);

43The result of evaluating ~x, where x is an expression of an enumeration type E with an underlying type U, is

44exactly the same as evaluating unchecked((E)(~(U)x)). This operator is only considered by unary

45operator overload resolution when the operand type is the enum type E (§14.2.3).

188

Chapter 14 Expressions

114.6.5 Prefix increment and decrement operators

2pre-increment-expression:

3++ unary-expression

4pre-decrement-expression:

5-- unary-expression

6The operand of a prefix increment or decrement operation shall be an expression classified as a variable, a

7property access, or an indexer access. The result of the operation is a value of the same type as the operand.

8If the operand of a prefix increment or decrement operation is a property or indexer access, the property or

9indexer shall have both a get and a set accessor. If this is not the case, a compile-time error occurs.

10Unary operator overload resolution (§14.2.3) is applied to select a specific operator implementation.

11Predefined ++ and -- operators exist for the following operand types: sbyte, byte, short, ushort, int,

12uint, long, ulong, char, float, double, decimal, and any enum type. The result type of each of these

13predefined operators is the same as the operand type. The predefined ++ operators return the value produced

14by adding 1 to the operand, and the predefined -- operators return the value produced by subtracting 1 from

15the operand. In a checked context, if the result of this addition or subtraction is outside the range of the

16result type, a System.OverflowException is thrown.

17There shall be an implicit conversion from the return type of the selected unary operator to the type of the

18primary-expression, otherwise a compile-time error occurs.

19The run-time processing of a prefix increment or decrement operation of the form ++x or --x consists of the

20following steps:

21If x is classified as a variable:

22o x is evaluated to produce the variable.

23o The value of x is converted to the operand type of the selected operator and the operator is invoked

24with this value as its argument.

25o The value returned by the operator is converted to the type of x. The resulting value is stored in the

26location given by the evaluation of x and becomes the result of the operation.

27If x is classified as a property or indexer access:

28o The instance expression (if x is not static) and the argument list (if x is an indexer access)

29associated with x are evaluated, and the results are used in the subsequent get and set accessor

30invocations.

31o The get accessor of x is invoked.

32o The value returned by the get accessor is converted to the operand type of the selected operator and

33operator is invoked with this value as its argument.

34o The value returned by the operator is converted to the type of x. The set accessor of x is invoked

35with this value as its value argument. This value also becomes the result of the operation.

36The ++ and -- operators also support postfix notation (§14.5.9). The result of x++ or x-- is the value of x

37before the operation, whereas the result of ++x or --x is the value of x after the operation. In either case, x

38itself has the same value after the operation.

39An operator ++ or operator -- implementation can be invoked using either postfix or prefix notation.

40It is not possible to have separate operator implementations for the two notations.

4114.6.6 Cast expressions

42A cast-expression is used to explicitly convert an expression to a given type.

43cast-expression:

44

( type ) unary-expression

189

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