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

C# LANGUAGE SPECIFICATION

1A property access or indexer access is always reclassified as a value by performing an invocation of the get-

2accessor or the set-accessor. The particular accessor is determined by the context of the property or indexer

3access: If the access is the target of an assignment, the set-accessor is invoked to assign a new value

4(§14.13.1). Otherwise, the get-accessor is invoked to obtain the current value (§14.1.1).

514.1.1 Values of expressions

6Most of the constructs that involve an expression ultimately require the expression to denote a value. In such

7cases, if the actual expression denotes a namespace, a type, or nothing, a compile-time error occurs.

8However, if the expression denotes a property access, an indexer access, or a variable, the value of the

9property, indexer, or variable is implicitly substituted:

10The value of a variable is simply the value currently stored in the storage location identified by the

11variable. A variable shall be considered definitely assigned (§12.3) before its value can be obtained, or

12otherwise a compile-time error occurs.

13The value of a property access expression is obtained by invoking the get-accessor of the property. If the

14property has no get-accessor, a compile-time error occurs. Otherwise, a function member invocation

15(§14.4.3) is performed, and the result of the invocation becomes the value of the property access

16expression.

17The value of an indexer access expression is obtained by invoking the get-accessor of the indexer. If the

18indexer has no get-accessor, a compile-time error occurs. Otherwise, a function member invocation

19(§14.4.3) is performed with the argument list associated with the indexer access expression, and the

20result of the invocation becomes the value of the indexer access expression.

2114.2 Operators

22Expressions are constructed from operands and operators. The operators of an expression indicate which

23operations to apply to the operands. [Example: Examples of operators include +, -, *, /, and new. Examples

24of operands include literals, fields, local variables, and expressions. end example]

25There are three kinds of operators:

26Unary operators. The unary operators take one operand and use either prefix notation (such as –x) or

27postfix notation (such as x++).

28Binary operators. The binary operators take two operands and all use infix notation (such as x + y).

29Ternary operator. Only one ternary operator, ?:, exists; it takes three operands and uses infix notation

30(c ? x : y).

31The order of evaluation of operators in an expression is determined by the precedence and associativity of

32the operators (§14.2.1).

33The order in which operands in an expression are evaluated, is left to right. [Example: In

34 F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old

35value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to

36operator precedence. end example]

37Certain operators can be overloaded. Operator overloading permits user-defined operator implementations to

38be specified for operations where one or both of the operands are of a user-defined class or struct type

39(§14.2.2).

4014.2.1 Operator precedence and associativity

41When an expression contains multiple operators, the precedence of the operators controls the order in which

42the individual operators are evaluated. [Note: For example, the expression x

43x + (y * z) because the * operator has higher precedence than the binary

+y * z is evaluated as

+operator. end note] The

44precedence of an operator is established by the definition of its associated grammar production. [Note: For

146

 

Chapter 14 Expressions

1

example, an additive-expression consists of a sequence of multiplicative-expressions separated by + or -

2

operators, thus giving the + and - operators lower precedence than the *, /, and % operators. end note]

3 The following table summarizes all operators in order of precedence from highest to lowest:

4

Subclause

Category

Operators

 

 

 

 

 

 

 

14.5

Primary

x.y f(x) a[x] x++ x-- new

 

 

typeof

checked

unchecked

 

 

 

 

 

 

 

 

14.6

Unary

+

-

!

~ ++x

--x

(T)x

 

 

 

 

 

 

 

 

 

14.7.1

Multiplicative

*

/

%

 

 

 

 

 

 

 

 

 

 

 

 

 

14.7

Additive

+

-

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.8

Shift

<<

>>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.9

Relational and

<

>

<=

>=

is

as

 

 

type-testing

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.9

Equality

==

!=

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.10

Logical AND

&

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.10

Logical XOR

^

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.10

Logical OR

|

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.11

Conditional

&&

 

 

 

 

 

 

 

AND

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.11

Conditional OR

||

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.12

Conditional

?:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.13

Assignment

=

*=

/=

%=

+=

-=

<<= >>= &= ^= |=

 

 

 

 

 

 

 

 

 

5

6When an operand occurs between two operators with the same precedence, the associativity of the operators

7controls the order in which the operations are performed:

8Except for the assignment operators, all binary operators are left-associative, meaning that operations

9

are performed from left to right. [Example: x + y + z is evaluated as (x + y) + z. end example]

10

The assignment operators and the conditional operator (?:) are right-associative, meaning that

11

operations are performed from right to left. [Example: x = y = z is evaluated as x = (y = z). end

12

example]

13

Precedence and associativity can be controlled using parentheses. [Example: x + y * z first multiplies y

14

by z and then adds the result to x, but (x + y) * z first adds x and y and then multiplies the result by z.

15end example]

1614.2.2 Operator overloading

17All unary and binary operators have predefined implementations that are automatically available in any

18expression. In addition to the predefined implementations, user-defined implementations can be introduced

19by including operator declarations in classes and structs (§17.9). User-defined operator implementations

20always take precedence over predefined operator implementations: Only when no applicable user-defined

21operator implementations exist will the predefined operator implementations be considered, as described in

22§14.2.3 and §14.2.4.

23The overloadable unary operators are:

24

+ - ! ~ ++ -- true false

147

C# LANGUAGE SPECIFICATION

1[Note: Although true and false are not used explicitly in expressions (and therefore are not included in

2the precedence table in §14.2.1), they are considered operators because they are invoked in several

3expression contexts: boolean expressions (§14.16) and expressions involving the conditional (§14.12), and

4conditional logical operators (§14.11). end note]

5The overloadable binary operators are:

6

+ - * / % & | ^ << >> == != > < >= <=

7Only the operators listed above can be overloaded. In particular, it is not possible to overload member

8access, method invocation, or the =, &&, ||, ?:, checked, unchecked, new, typeof, as, and

9is operators.

10When a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly

11overloaded. [Example: An overload of operator * is also an overload of operator *=. This is described

12further in §14.13. end example] The assignment operator itself (=) cannot be overloaded. An assignment

13always performs a simple bit-wise copy of a value into a variable.

14Cast operations, such as (T)x, are overloaded by providing user-defined conversion operators (§13.4).

15Element access, such as a[x], is not considered an overloadable operator. Instead, user-defined indexing is

16supported through indexers (§17.8).

17In expressions, operators are referenced using operator notation, and in declarations, operators are referenced

18using functional notation. The following table shows the relationship between operator and functional

19notations for unary and binary operators. In the first entry, op denotes any overloadable unary prefix

20operator. In the second entry, op denotes the unary postfix ++ and -- operators. In the third entry, op

21denotes any overloadable binary operator. [Note: For an example of overloading the ++ and -- operators see

22§17.9.1. end note]

23

Operator notation

Functional notation

 

 

op x

operator op(x)

 

 

x op

operator op(x)

 

 

x op y

operator op(x, y)

 

 

24

25User-defined operator declarations always require at least one of the parameters to be of the class or struct

26type that contains the operator declaration. [Note: Thus, it is not possible for a user-defined operator to have

27the same signature as a predefined operator. end note]

28User-defined operator declarations cannot modify the syntax, precedence, or associativity of an operator.

29[Example: The / operator is always a binary operator, always has the precedence level specified in §14.2.1,

30and is always left-associative. end example]

31[Note: While it is possible for a user-defined operator to perform any computation it pleases,

32implementations that produce results other than those that are intuitively expected are strongly discouraged.

33For example, an implementation of operator == should compare the two operands for equality and return

34an appropriate bool result. end note]

35The descriptions of individual operators in §14.5 through §14.13 specify the predefined implementations of

36the operators and any additional rules that apply to each operator. The descriptions make use of the terms

37unary operator overload resolution, binary operator overload resolution, and numeric promotion,

38definitions of which are found in the following subclauses.

3914.2.3 Unary operator overload resolution

40An operation of the form op x or x op, where op is an overloadable unary operator, and x is an expression of

41type X, is processed as follows:

148

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