Operator Precedence

Say you wanted to add 10 to 15 and multiply the result by 4; you might use this expression:

10 + 15 * 4

And you might expect the result to be 100. Unfortunately, C# will give you a value of 70. What happened? Instead of adding 10 to 15 and multiplying the result by 4, C# performed the multiplication first, multiplying 15 and 4 and adding the result (60) to 10 to get 70. That's because multiplication has higher precedence than addition, so the multiplication operation in this example was performed first. You can see the C# operator precedence in Table 1.8, from highest to lowest.

Table 1.8. C# Operators Precedence from Highest to Lowest
CATEGORYOPERATORS
Primaryx.y f(x) a[x] x++ x-- new typeof checked unchecked
Unary+ - ! ~ ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.