Behind the Scenes Look at C#: Operators - Operator Precedence
(Page 8 of 8 )
It's very common to have more than one operator in your expressions. For example, here is an expression with several operators: 2 + 3 * 4. What's the result of this expression? If we add 2 and 3 then multiply by 4, the product will be 20. If we multiply 3 by 4 and then add 2 the product will be 14. Which one is true? Logically both of them are true because 2 + 3 = 5 multiplied by 4 = 20 and 3 multipled by 4 = 12 added to 2 = 14. So we have two operators in the expression, but we need a way to tell which one will be evaluated first. There are two procedures for doing this. The first way is to use parentheses () to tell the C# compiler that you want to evaluate the enclosed expression first, as in the following code example:
namespace Operators
{
class Class1
{
static void Main(string[] args)
{
Console.WriteLine((2 + 3) * 4);
Console.WriteLine(2 + (3 * 4));
Console.ReadLine();
}
}
}
The result in the console window will be:

Our problem has been solved with the parentheses. The parentheses just determine the order of evaluation for the operators in the expression, so the enclosed expression evaluates first and then the outer expression. So for this example, (2 + 3) = 5 then multiplied by 4 = 20 because the compiler evaluates the enclosed expression first and multiplies the result.
The process of the order of evaluation of operators is called operators precedence. If you don't want to use the parentheses, and would rather rely on the second procedure, it is the default C# operator precedence. The C# compiler evaluates operators based on the following table:
The IntelliSense Operator . |
The Parentheses Operator () |
| Array Index Operator [] |
| The Post Increment Operator ++ |
| The Post Decrement Operator -- |
| The new Operator |
| The typeof Operator |
| The checked Operator |
| the unchecked Operator |
| The unary plus + |
| The unary minus - |
| the unary ! |
| The unary ~ |
| The Pre Increment Operator ++ |
| The Pre Decrement Operator -- |
| The multiplication Operator * |
| The division Operator / |
| The Module Operator % |
| The Addition Operator + |
| The Subtraction Operator - |
| The Shift-Left Operator << |
| The Shift-Right Operator >> |
| The Less Than Operator < |
| The Less Than or Equal Operator <= |
| The Greater Than Operator > |
| The Greater Than or Equal Operator >= |
| The is Operator |
| The Equality Operator == |
| The Is Not Equal Operator != |
| The And Operator & |
| The XOR Operator ^ |
| The Or Operator | |
| The Short-Circuit And Operator && |
| The Short-Circuit Or Operator || |
| The Ternary Operator ?: |
| The Assignment Operators as following =,*=,/=,+=,-=,<<=,>>=,&=,^= and |= |
Using this table you can tell that the C# compiler will do the following:
Multiply 4 by 3 because the * operator has a higher precedence than the + operator (the operator precedence table lists the C# operators from the highest to lowest precedence).
Add the product to 2, which equals 14, and that's all.
In the next article we will examine the concepts behind operator overloading.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |