Skip to Content
Microsoft® Visual C#® .NET 2003 Kick Start
book

Microsoft® Visual C#® .NET 2003 Kick Start

by Steven Holzner
June 2003
Intermediate to advanced
624 pages
12h 41m
English
Sams
Content preview from Microsoft® Visual C#® .NET 2003 Kick Start

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+ - ! ~ ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Microsoft® .NET Kick Start

Microsoft® .NET Kick Start

Hitesh Seth

Publisher Resources

ISBN: 0672325470Purchase book