Why is C C Sharp

C # Operators and Expressions (C # Reference)

C # offers many different operators. Many of these operators are supported by the built-in types and allow you to perform basic operations on the values ​​of these types. These operators are divided into the following groups:

Usually you can overload these operators, that is, specify the operator behavior for the operands of a user-defined type.

The simplest C # expressions are literals (for example, integers and real numbers) and names of variables. You can combine these into complex expressions using operators. Operator precedence and associativity determine the order in which operations in an expression are performed. You can use parentheses to change the order of evaluation, which is determined by operator precedence and associativity.

The following code is the example of the expressions on the right side of the assignments:

Usually an expression produces a result and can be enclosed in another expression. A method call is an example of an expression that does not produce a result. It can only be used as a statement like the following example.

Here are a few more expressions provided by C #:

  • Interpolated string expressions that provide convenient syntax for creating formatted strings:

  • Lambda expressions that you can use to create anonymous functions:

  • Query expressions that let you use query functions directly in C #:

You can use an expression body function to provide a precise definition for a method, constructor, property, indexer, or finalizer.

Operator precedence

In a multi-operator expression, the higher precedence operators are evaluated before the lower precedence operators. In the following example, multiplication is done first because it has a higher precedence than addition:

Use parentheses to change the order of evaluation, which is determined by operator precedence:

The following table lists the C # operators, from highest to lowest precedence. The operators on each line have the same precedence.

OperatorsCategory or name
x.y, f (x), a [i],,, x ++, x--, x !, new, typeof, checked, unchecked, default, nameof, delegate, sizeof, stackalloc, x-> yPrimary
+ x, -x,! x, ~ x, ++ x, --x, ^ x, (T) x, await, & x, * x, true and falseUnary
x * y, x / y, x% yMultiplicative
x + y, x - yAdditive
x << y, x >> yShift
x y, x <= y, x> = y, is, asRelational and type test
x == y, x! = yequality
Boolean logical AND operator or bitwise logical AND operator
Boolean logical XOR operator or bitwise logical XOR operator
Boolean logical OR operator or bitwise logical OR operator
x && yConditional AND
x || yConditional OR
x ?? yZero composition operator
c? t: fConditional operator
x = y, x + = y, x - = y, x * = y, x / = y, x% = y, x & = y, x | = y, x ^ = y, x << = y, x >> = y, x ?? = y, =>Assignment and lambda declaration

Operator associativity

When operators have the same precedence, the associativity of the operators determines the order in which operations are performed:

  • Left Associative Operators are evaluated from left to right. With the exception of the assignment operators and the NULL collection operator, all binary operators are left-associative. is evaluated as, for example.
  • Legal associative Operators are evaluated from right to left. The assignment operators, the NULL collection operators, and the conditional operator are right-associative. is evaluated as, for example.

Use parentheses to change the order of evaluation determined by operator associativity:

Operand evaluation

Regardless of operator precedence and associativity, the operands in an expression are evaluated from left to right. The following examples illustrate the order in which operators and operands are evaluated:

expressionOrder of evaluation
a, b, +
a, b, c, *, +
a, b, /, c, d, *, +
a, b, c, +, /, d, *

As a rule, all operands of an operator are evaluated. However, some operators evaluate operands conditionally. That is, the value of the operand on the far left in such an operator defines whether (or which) other operands should be evaluated. These operators are the conditional logical operators AND () and OR (), the NULL collective operators and, the NULL-conditional operators and and the conditional operator. See the description of each operator for more information.

C # language specification

For more information, see the following sections of the C # language specification:

See also

Is this page helpful?