Expressions and Operators in C#


This article has been excerpted from book "Visual C# Programmer's Guide"

An expression, constructed of a sequence of operators and operands, specifies computation of a value or designates a variable or constant. The operators of an expression indicate which operations to apply to the operands. Examples of operators include +, -, *, /, and new. Examples of operands include literals, fields, local variables, and expressions.

Most of the constructs that involve an expression ultimately require the expression to denote a value. In such cases, an error occurs if the actual expression denotes a namespace, a type, a method group, or nothing. However, if the expression denotes a property access, an indexer access, or a variable, the value of the property, indexer, or variable is implicitly substituted.

The C# Language Specification includes just three types of operators:

  • Unary operators take one operand and use either prefix notation (such as –x) or postfix notation (such as x++).
  • Binary operators take two operands and use infix notation (such as x + y).
  • The lone ternary operator (?:) takes three operands and uses infix notation (such as c? x: y).
Certain operators can be overloaded. Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user defined class or struct type.

The order of evaluation of operators in an expression is determined by the precedence and associatively of the operators. When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. For example, the x + y * z is evaluated as x + (y * z) because the * operator has higher precedence than the + operator.

Table 5.4 lists all operators, grouped by category and presented in order of precedence, from highest to lowest.

table5.4.gif

Table 5.4: Operator Priority

The precedence of an operator is established by the definition of its associated grammar production in the C# Language Specification (http://msdn.microsoft.com/net/ecma/). For example, an additive expression consists of a sequence of multiplicative expressions separated by + or - operators, thus giving the + and - operators lower precedence than the *, /, and % operators.

When an operand occurs between two operators with the same precedence, the associatively of the operators controls the order in which the operations are performed.

Except for the assignment operators, all binary operators are left-associative, meaning that operations are performed from left to right. For example, x + y + z is evaluated as (x + y) + z. The assignment operators and the conditional operator (?:) are right-associative, meaning that operations are performed from right to left. For example, x = y = z is evaluated as x = (y = z).

The evaluation of operands is separate from and unrelated to operator precedence. Operands in an expression are evaluated from left to right. Consider the following expression:

F(i) + G(i++) * H(i)

In this example, method F is called using the old value of i; then method G is called with the old value of i (in this case, the post-increment operator was used); and, finally, method H is called with the new value of i.

Conclusion

Hope this article would have helped you in understanding expression and operator. See other articles on the website on .NET and C#.

visual C-sharp.jpg The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

Up Next
    Ebook Download
    View all
    Learn
    View all