Definition - What does Precedence mean?
Precedence, in C#, is the rule that specifies the order in which certain operations need to be performed in an expression. For a given expression containing more than two operators, it determines which operations should be calculated first.
While the evaluation of an expression that is performed by humans starts from on the left and works toward the right, a C# expression is evaluated through operator precedence. Precedence helps to describe an expression that contains different kinds of operators without parentheses as an expression with parentheses by applying the predefined rules set by the C# Language Specification (CLS).
A C# expression is a sequence of one or more operands that are combined using operators and can also contain literal values, a method invocation, variables, etc. This type of expression can be evaluated as a single value, object or method. Precedence plays a vital role in determining the order in which an expression has to be evaluated.
Techopedia explains Precedence
The precedence of C# operators is similar to that used in algebraic formulas. The list of categories of C# operators with the order of precedence includes:
- Primary ( x.y, f(x), a[x], x++, x--, new, typeof, checked, unchecked)
- Unary (+, -, !, ~, ++x, --x, (T)x)
- Multiplicative (*, /, %)
- Additive (+, -)
- Shift (<<. >>)
- Relational and type testing (<. >, <=, >=, is, as)
- Equality (==,!= )
- Logical AND (&)
- Logical XOR (^)
- Logical OR (|)
- Conditional AND (&&)
- Conditional OR ||
- Null coalescing (??)
- Ternary (or Conditional) (?:)
- Assignment (=, *=, /=, %=. +=, -=, , &=, ^=, |=0)
For example, the expression "a - b/c, b/c" will be evaluated first and then the quotient will be subtracted from "a" because division has a higher precedence than subtraction.
By using parentheses for the expression that needs to be evaluated first, precedence can be overridden.
Techopedia Deals: The Complete Android Developer Course - Build 14 Apps
Join thousands of others with our weekly newsletter
The 4th Era of IT Infrastructure: Superconverged Systems:
Approaches and Benefits of Network Virtualization:
Free E-Book: Public Cloud Guide:
Free Tool: Virtual Health Monitor:
Free 30 Day Trial – Turbonomic: