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

Chapter 14 Expressions

1The set of candidate user-defined operators provided by X for the operation operator op(x) is

2determined using the rules of §14.2.5.

3If the set of candidate user-defined operators is not empty, then this becomes the set of candidate

4operators for the operation. Otherwise, the predefined unary operator op implementations become the

5set of candidate operators for the operation. If type X is not an enum type, then any predefined unary

6operator with a parameter type that is an enum type is removed from consideration. The predefined

7implementations of a given operator are specified in the description of the operator (§14.5 and

8§14.5.13).

9The overload resolution rules of §14.4.2 are applied to the set of candidate operators to select the best

10operator with respect to the argument list (x), and this operator becomes the result of the overload

11resolution process. If overload resolution fails to select a single best operator, a compile-time error

12occurs.

1314.2.4 Binary operator overload resolution

14An operation of the form x op y, where op is an overloadable binary operator, x is an expression of type X,

15and y is an expression of type Y, is processed as follows:

16The set of candidate user-defined operators provided by X and Y for the operation operator op(x, y)

17is determined. The set consists of the union of the candidate operators provided by X and the candidate

18operators provided by Y, each determined using the rules of §14.2.5. If X and Y are the same type, or if X

19and Y are derived from a common base type, then shared candidate operators only occur in the combined

20set once.

21If the set of candidate user-defined operators is not empty, then this becomes the set of candidate

22operators for the operation. Otherwise, the predefined binary operator op implementations become the

23set of candidate operators for the operation. If neither X nor Y is an enum type, then any predefined

24binary operator with a parameter type that is an enum type is removed from consideration. Similarly, if

25neither X nor Y is a delegate type, then any predefined binary operator with a parameter type that is a

26delegate type is removed from consideration. The predefined implementations of a given operator are

27specified in the description of the operator (§14.7 through §14.13).

28The overload resolution rules of §14.4.2 are applied to the set of candidate operators to select the best

29operator with respect to the argument list (x, y), and this operator becomes the result of the overload

30resolution process. If overload resolution fails to select a single best operator, a compile-time error

31occurs.

3214.2.5 Candidate user-defined operators

33Given a type T and an operation operator op(A), where op is an overloadable operator and A is an

34argument list, the set of candidate user-defined operators provided by T for operator op(A) is

35determined as follows:

36For all operator op declarations in T, if at least one operator is applicable (§14.4.2.1) with respect to

37the argument list A, then the set of candidate operators consists of all applicable operator op

38declarations in T.

39Otherwise, if T is object, the set of candidate operators is empty.

40Otherwise, the set of candidate operators provided by T is the set of candidate operators provided by the

41direct base class of T.

4214.2.6 Numeric promotions

43This subclause is informative.

44Numeric promotion consists of automatically performing certain implicit conversions of the operands of the

45predefined unary and binary numeric operators. Numeric promotion is not a distinct mechanism, but rather

46an effect of applying overload resolution to the predefined operators. Numeric promotion specifically does

149

C# LANGUAGE SPECIFICATION

1not affect evaluation of user-defined operators, although user-defined operators can be implemented to

2exhibit similar effects.

3As an example of numeric promotion, consider the predefined implementations of the binary * operator:

4int operator *(int x, int y);

5uint operator *(uint x, uint y);

6long operator *(long x, long y);

7ulong operator *(ulong x, ulong y);

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

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

10float operator *(float x, float y);

11double operator *(double x, double y);

12decimal operator *(decimal x, decimal y);

13When overload resolution rules (§14.4.2) are applied to this set of operators, the effect is to select the first of

14the operators for which implicit conversions exist from the operand types. [Example: For the operation

15b * s, where b is a byte and s is a short, overload resolution selects operator *(int, int) as the

16best operator. Thus, the effect is that b and s are converted to int, and the type of the result is int.

17Likewise, for the operation i * d, where i is an int and d is a double, overload resolution selects

18operator *(double, double) as the best operator. end example]

19End of informative text.

2014.2.6.1 Unary numeric promotions

21This subclause is informative.

22Unary numeric promotion occurs for the operands of the predefined +, , and ~ unary operators. Unary

23numeric promotion simply consists of converting operands of type sbyte, byte, short, ushort, or char

24to type int. Additionally, for the unary operator, unary numeric promotion converts operands of type

25uint to type long.

26End of informative text.

2714.2.6.2 Binary numeric promotions

28This subclause is informative.

29Binary numeric promotion occurs for the operands of the predefined +, , *, /, %, &, |, ^, ==, !=, >, <, >=,

30and <= binary operators. Binary numeric promotion implicitly converts both operands to a common type

31which, in case of the non-relational operators, also becomes the result type of the operation. Binary numeric

32promotion consists of applying the following rules, in the order they appear here:

33If either operand is of type decimal, the other operand is converted to type decimal, or a compile-

34time error occurs if the other operand is of type float or double.

35Otherwise, if either operand is of type double, the other operand is converted to type double.

36Otherwise, if either operand is of type float, the other operand is converted to type float.

37Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a

38compile-time error occurs if the other operand is of type sbyte, short, int, or long.

39Otherwise, if either operand is of type long, the other operand is converted to type long.

40Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int,

41both operands are converted to type long.

42Otherwise, if either operand is of type uint, the other operand is converted to type uint.

43Otherwise, both operands are converted to type int.

44[Note: The first rule disallows any operations that mix the decimal type with the double and float

45types. The rule follows from the fact that there are no implicit conversions between the decimal type

46and the double and float types. end note]

150

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