Parentheses and Precedence

If the precedence and associativity rules do not correspond with your calculations, you can apply the parentheses operator ( <Numerical_expression> ) to control the order in which each operator is applied in an arithmetic expression.

Syntax Box 7.3 The Parentheses Operator

						Parenthesized_expression ::=

                 ( <Numerical_expression> )

Parentheses in C# are used in the same way as in algebra and arithmetic to accomplish this task. Because of their higher precedence level (as shown in Table 7.1), they have first priority over any precedence rules set for most other operators. The next example illustrates.

We want to construct a statement that finds the average time two people spend exercising every day. Our first attempt, ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.