Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp 4 Language Specification.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
2.51 Mб
Скачать

7.3Operators

Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. Examples of operators include +, -, *, /, and new. Examples of operands include literals, fields, local variables, and expressions.

There are three kinds of operators:

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

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

  • Ternary operator. Only one ternary operator, ?:, exists; it takes three operands and uses infix notation (c? x: y).

The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators (§7.3.1).

Operands in an expression are evaluated from left to right. For example, in F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to operator precedence.

Certain operators can be overloaded. Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type (§7.3.2).

7.3.1Operator precedence and associativity

When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. For example, the expression x + y * z is evaluated as x + (y * z) because the * operator has higher precedence than the binary + operator. The precedence of an operator is established by the definition of its associated grammar production. For example, an additive-expression consists of a sequence of multiplicative-expressions separated by + or - operators, thus giving the + and - operators lower precedence than the *, /, and % operators.

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

Section

Category

Operators

7.6

Primary

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

typeof default checked unchecked delegate

7.7

Unary

+ - ! ~ ++x --x (T)x

7.8

Multiplicative

* / %

7.8

Additive

+ -

7.9

Shift

<< >>

7.10

Relational and type testing

< > <= >= is as

7.10

Equality

== !=

7.11

Logical AND

&

7.11

Logical XOR

^

7.11

Logical OR

|

7.12

Conditional AND

&&

7.12

Conditional OR

||

7.13

Null coalescing

??

7.14

Conditional

?:

7.17, 7.15

Assignment and lambda expression

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

=>

When an operand occurs between two operators with the same precedence, the associativity of the operators controls the order in which the operations are performed:

  • Except for the assignment operators, all binary operators are left-associative, meaning that operations are performed from left to right. For example, x + y + z is evaluated as (x + y) + z.

  • The assignment operators and the conditional operator (?:) are right-associative, meaning that operations are performed from right to left. For example, x = y = z is evaluated as x = (y = z).

Precedence and associativity can be controlled using parentheses. For example, x + y * z first multiplies y 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.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]