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

Chapter 14 Expressions

1class Test

2{

3

static void Main() {

 

4

string s = null;

 

5

Console.WriteLine("s = >" + s + "<"); // displays s = ><

6

int i = 1;

 

7

Console.WriteLine("i = " + i);

// displays i = 1

8

float f = 1.2300E+15F;

 

9

Console.WriteLine("f = " + f);

// displays f = 1.23E+15

10

decimal d = 2.900m;

 

11

Console.WriteLine("d = " + d);

// displays d = 2.900

12}

13}

14end example]

15The result of the string concatenation operator is a string that consists of the characters of the left

16operand followed by the characters of the right operand. The string concatenation operator never returns

17a null value. A System.OutOfMemoryException can be thrown if there is not enough memory

18available to allocate the resulting string.

19Delegate combination. Every delegate type implicitly provides the following predefined operator, where

20D is the delegate type:

21D operator +(D x, D y);

22The binary + operator performs delegate combination when both operands are convertible to some

23delegate type D. (If the operands have different delegate types, overload resolution finds no applicable

24operator and a compile-time error occurs.) This operator is only considered by overload resolution

25(§14.2.4) when one of the actual operands is of type D. If the first operand is null, the result of the

26operation is the value of the second operand (even if that is also null). Otherwise, if the second operand

27is null, then the result of the operation is the value of the first operand. Otherwise, the result of the

28operation is a new delegate instance whose invocation list consists of the elements in the invocation list

29of the first operand, followed by the elements in the invocation list of the second operand. That is, the

30invocation list of the resulting delegate is the concatenation of the invocation lists of the two operands.

31[Note: For examples of delegate combination, see §14.7.5 and §22.3. Since System.Delegate is not a

32delegate type, operator + is not defined for it. end note]

3314.7.5 Subtraction operator

34For an operation of the form x – y, binary operator overload resolution (§14.2.4) is applied to select a

35specific operator implementation. The operands are converted to the parameter types of the selected

36operator, and the type of the result is the return type of the operator.

37The predefined subtraction operators are listed below. The operators all subtract y from x.

38Integer subtraction:

39int operator –(int x, int y);

40uint operator –(uint x, uint y);

41long operator –(long x, long y);

42ulong operator –(ulong x, ulong y);

43void operator -(long x, ulong y);

44void operator -(ulong x, long y);

45The operators with void return type always produce a compile-time error. Consequently, it is an error

46for one operand to be of type long and the other to be of type ulong.

47In a checked context, if the difference is outside the range of the result type, a

48System.OverflowException is thrown. In an unchecked context, overflows are not reported and

49any significant high-order bits outside the range of the result type are discarded.

50Floating-point subtraction:

51float operator –(float x, float y);

52double operator –(double x, double y);

195

C# LANGUAGE SPECIFICATION

1The difference is computed according to the rules of IEC 60559 arithmetic. The following table lists the

2results of all possible combinations of nonzero finite values, zeros, infinities, and NaNs. In the table, x

3and y are nonzero finite values, and z is the result of x – y, rounded to the nearest representable value.

4

If x and y are equal, z is positive zero. If the magnitude of x – y is too large to represent in the

5

destination type, z is an infinity with the same sign as x – y.

 

 

 

6

 

 

 

 

 

 

 

 

 

 

 

 

 

y

+0

–0

+∞

 

–∞

NaN

 

 

 

 

 

 

 

 

 

 

 

 

 

 

x

+z

+x

+x

–∞

 

+∞

NaN

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+0

–y

+0

+0

–∞

 

+∞

NaN

 

 

 

 

 

 

 

 

 

 

 

 

 

 

–0

–y

–0

+0

–∞

 

+∞

NaN

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+∞

+∞

+∞

+∞

NaN

 

+∞

NaN

 

 

 

 

 

 

 

 

 

 

 

 

 

 

–∞

–∞

–∞

–∞

–∞

 

NaN

NaN

 

 

 

 

 

 

 

 

 

 

 

 

 

 

NaN

NaN

NaN

NaN

NaN

 

NaN

NaN

 

7

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8Decimal subtraction:

9decimal operator –(decimal x, decimal y);

10If the magnitude of the resulting value is too large to represent in the decimal format, a

11System.OverflowException is thrown. The scale of the result, before any rounding, is the larger of

12the scales of the two operands.

13Decimal subtraction is equivalent to using the subtraction operator of type System.Decimal.

14Enumeration subtraction. Every enumeration type implicitly provides the following predefined operator,

15where E is the enum type, and U is the underlying type of E:

16U operator –(E x, E y);

17This operator is evaluated exactly as (U)((U)x – (U)y). In other words, the operator computes the

18difference between the ordinal values of x and y, and the type of the result is the underlying type of the

19enumeration.

20E operator –(E x, U y);

21This operator is evaluated exactly as (E)((U)x – y). In other words, the operator subtracts a value

22from the underlying type of the enumeration, yielding a value of the enumeration.

23These operators are only considered by overload resolution (§14.2.4) when one of the actual operands is

24of type E.

25Delegate removal. Every delegate type implicitly provides the following predefined operator, where D is

26the delegate type:

27D operator –(D x, D y);

28The binary - operator performs delegate removal when both operands are convertible to some delegate

29type D. (If the operands have different delegate types, overload resolution finds no applicable operator

30and a compile-time error occurs.) This operator is only considered by overload resolution (§14.2.4)

31when one of the actual operands is of type D. If the first operand is null, the result of the operation is

32null. Otherwise, if the second operand is null, then the result of the operation is the value of the first

33operand. Otherwise, both operands represent invocation lists (§22.1) having one or more entries, and the

34result is a new invocation list consisting of the first operand’s list with the second operand’s entries

35removed from it, provided the second operand’s list is a proper contiguous sublist of the first’s. (For

36determining sublist equality, corresponding entries are compared as for the delegate equality operator

37(§14.9.8).) Otherwise, the result is the value of the left operand. Neither of the operands’ lists is changed

38in the process. If the second operand’s list matches multiple sublists of contiguous entries in the first

39operand’s list, the right-most matching sublist of contiguous entries is removed. If removal results in an

40empty list, the result is null. [Example:

196

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