Precedence

Why Trust Techopedia

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.

Advertisements

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.

Advertisements

Related Terms

Margaret Rouse
Technology Specialist
Margaret Rouse
Technology Specialist

Margaret is an award-winning writer and educator known for her ability to explain complex technical topics to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles in the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret’s idea of ​​a fun day is to help IT and business professionals to learn to speak each other’s highly specialized languages.