Partager via


Operators (C# Programming Guide) 

In C#, an operator is a term or a symbol that takes one or more expressions, called operands, as input and returns a value. Operators that take one operand, such as the increment operator (++) or new, are called unary operators. Operators that take two operands, such as arithmetic operators (+,-,*,/) are called binary operators. One operator, the conditional operator (?:), takes three operands and is the sole tertiary operator in C#.

The following C# statement contains a single unary operator, and a single operand. The increment operator, ++, modifies the value of the operand y.:

y++;

The following C# statement contains two binary operators, each with two operands. The assignment operator, =, has the integer y, and the expression 2 + 3 as operands. The expression 2 + 3 itself contains the addition operator, and uses the integer values 2 and 3 as operands:

y = 2 + 3;

An operand can be a valid expression of any size, composed of any number of other operations.

Operators in an expression are evaluated in a specific order known as operator precedence. The following table divides the operators into categories based on the type of operation they perform. The categories are listed in order of precedence.

Primary

x.y, f(x), a[x], x++, x--, new, typeof, checked, unchecked

Unary

+, -, !, ~, ++x, --x, (T)x

Arithmetic — Multiplicative

*, /, %

Arithmetic — Additive

+, -

Shift

<<, >>

Relational and type testing

<, >, <=, >=, is, as

Equality

==, !=

Logical, in order of precedence

&, ^, |

Conditional, in order of precedence

&&, ||, ?:

Assignment

=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

When two operators with the same precedence are present in an expression, they are evaluated based on associativity. Left-associative operators are evaluated in order from left to right. For example, x * y / z is evaluated as (x * y) / z. Right-associative operators are evaluated in order from right to left. The assignment operators and the tertiary operator (?:) are right-associative. All other binary operators are left-associative. However, C# standard does not specify when, in an expression, the "set" portion of an increment instruction is executed. For example, the output of the following example code is 6:

int num1 = 5;
num1++;
System.Console.WriteLine(num1);

However, the output of the following example code is undefined:

int num2 = 5;
num2 = num2++;  //not recommended
System.Console.WriteLine(num2);

Therefore, the latter example is not recommended. Parentheses can be used to surround an expression and force that expression to be evaluated before any others. For example, 2 + 3 * 2 would normally become 8. This is because multiplicative operators take precedence over additive operators. Writing the expression as (2 + 3 ) * 2 results in 10, because it indicates to the C# compiler that the addition operator (+) must be evaluated before the multiplication operator (*).

You can change the behavior of operators for custom classes and structs. This process is called operator overloading. For more information, see Overloadable Operators (C# Programming Guide).

For more information, see Operator Keywords (C# Reference) and C# Operators.

See Also

Reference

Statements, Expressions, and Operators (C# Programming Guide)

Concepts

C# Programming Guide