Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
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.
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:
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.