[ Team LiB ] |
2.8 Expressions and OperatorsAn expression is a sequence of operators and operands that specifies a computation. C# has unary operators, binary operators, and one ternary operator. Complex expressions can be built because an operand may itself be an expression, such as the operand (1 + 2) shown in the following example: ((1 + 2) / 3) 2.8.1 Operator PrecedenceWhen an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. When the operators are of the same precedence, their associativity determines the order. Binary operators (except for assignment operators) are left-associative; i.e., they are evaluated from left to right. The assignment operators, unary operators, and the conditional operator are right-associative; i.e., they are evaluated from right to left. For example: 1 + 2 + 3 * 4 is evaluated as: ((1 + 2) + (3 * 4)) because * has a higher precedence than +, and + is a binary operator that is left-associative. You can insert parentheses to change the default order of evaluation. C# overloads operators, which means the same operator may have different meanings for different types. Table 2-2 lists C#'s operators in order of precedence. Operators in the same box have the same precedence, and operators in italic may be overloaded for custom types.
2.8.2 Arithmetic Overflow Check OperatorsThe syntax for the checked and unchecked operators is: checked (expression) unchecked (expression) The syntax for the checked and unchecked statements is: checked statement-or-statement-block unchecked statement-or-statement-block The checked operator tells the runtime to generate an OverflowException if an integral expression exceeds the arithmetic limits of that type. The checked operator affects expressions with the ++, --, (unary)-, +, -, *, /, and explicit conversion operators between integral types. For example: int a = 1000000; int b = 1000000; // Check an expression int c = checked(a*b); // Check every expression in a statement-block checked { ... c = a * b; ... } The checked operator only applies to runtime expressions, since constant expressions are checked during compilation (though this can be turned off with the /checked [+|-] command-line switch). The unchecked operator disables arithmetic checking at compile time, and is seldom useful, but does make expressions such as the following compile: const int signedBit = unchecked((int)0x80000000); |
[ Team LiB ] |