Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
19
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 8 EXPRESSIONS AND OPERATORS

Order of Evaluation

An expression can be made up of many nested subexpressions. The order in which the subexpressions are evaluated can make a difference in the final value of the expression.

For example, given the expression 3 * 5 + 2, there are two possible results depending on the order in which the subexpressions are evaluated, as shown in Figure 8-3.

If the multiplication is performed first, the result is 17.

If the 5 and the 2 are added together first, the result is 21.

Figure 8-3. Simple order of evaluation

Precedence

You know from your grade-school days that in the preceding example, the multiplication must be performed before the addition because multiplication has a higher precedence than addition. But unlike grade-school days, when you had four operators and two levels of precedence, things are a bit more complex with C#, which has more than 45 operators and 14 levels of precedence.

Table 8-4 shows the complete list of operators and their precedences. The table lists the highest precedence operators at the top and continues to the lowest precedence operators at the bottom.

Table 8-4. Operator Precedence: Highest to Lowest

Category

Operators

Primary

a.x, f(x), a[x], x++, x--, new, typeof, checked, unchecked

Unary

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

Multiplicative

*, /, %

Additive

+, -

Shift

<<, >>

Relational and type

<, >, <=, >=, is, as

Equality

==, !=

Logical AND

&

Logical XOR

^

 

 

209

CHAPTER 8 EXPRESSIONS AND OPERATORS

Category

Operators

Logical OR

|

Conditional AND

&&

Conditional OR

||

Conditional

?:

Assignment

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

 

 

Associativity

If all the operators in an expression have different levels of precedence, then evaluate each subexpression, starting at the one with the highest level, and work down the precedence scale.

But what if two sequential operators have the same level of precedence? For example, given the expression 2 / 6 * 4, there are two possible evaluation sequences:

(2 / 6) * 4 = 4/3

or

2 / (6 * 4) = 1/12

When sequential operators have the same level of precedence, the order of evaluation is determined by operator associativity. That is, given two operators of the same level of precedence, one or the other will have precedence, depending on the operators’ associativity. Some important characteristics of operator associativity are the following and are summarized in Table 8-5:

Left-associative operators are evaluated from left to right.

Right-associative operators are evaluated from right to left.

Binary operators, except the assignment operators, are left-associative.

The assignment operators and the conditional operator are right-associative.

Therefore, given these rules, the preceding example expression should be grouped left to right, giving (2 / 6 ) * 4, which yields 4/3.

210

CHAPTER 8 EXPRESSIONS AND OPERATORS

Table 8-5. Summary of Operator Associativity

Type of Operator

Associativity

Assignment operators

Right-associative

Other binary operators

Left-associative

The conditional operator

Right-associative

 

 

You can explicitly set the order of evaluation of the subexpressions of an expression by using parentheses. Parenthesized subexpressions do the following:

Override the precedence and associativity rules

Are evaluated in order from the innermost nested set to the outermost

211

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