Operators (C# Programming Guide)
Operators (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/ms173145
y++;
The following C# statement contains two binary operators, each with two operands. The assignment operator, =, has the integer variable y and the expression 2 + 3 as operands. The expression 2 + 3 itself consists of the addition operator and two operands, 2 and 3.
y = 2 + 3;
n1 = 11 - 2 * 4;
The multiplication is executed first because multiplication takes precedence over subtraction. The following table separates the operators into categories based on the type of operation they perform. The categories are listed in order of precedence. Primary Operators
Expression
x.y
Description
Member access
1 of 7
5/25/2012 11:41 AM
http://msdn.microsoft.com/en-us/library/ms173145
f(x) a[x] x++ x-new T(...) new T(...){...} new {...} new T[...] typeof(T) checked(x) unchecked(x) default (T) delegate {}
Method and delegate invocation Array and indexer access Post-increment Post-decrement Object and delegate creation Object creation with initializer. See Object and Collection Initializers (C# Programming Guide). Anonymous object initializer. See Anonymous Types (C# Programming Guide). Array creation. See Arrays (C# Programming Guide). Obtain System.Type object for T Evaluate expression in checked context Evaluate expression in unchecked context Obtain default value of type T Anonymous function (anonymous method)
Unary Operators
Expression
+x -x !x ~x ++x --x (T)x Multiplicative Operators
Description
Identity Negation Logical negation Bitwise negation Pre-increment Pre-decrement Explicitly convert x to type T
2 of 7
5/25/2012 11:41 AM
http://msdn.microsoft.com/en-us/library/ms173145
Expression
* / % Additive Operators
Description
Multiplication Division Remainder
Expression
x+y x-y Shift Operators
Description
Addition, string concatenation, delegate combination Subtraction, delegate removal
Expression
x << y x >> y Relational and Type Operators
Description
Shift left Shift right
Expression
x<y x>y x <= y x >= y x is T x as T Equality Operators
Description
Less than Greater than Less than or equal Greater than or equal Return true if x is a T, false otherwise Return x typed as T, or null if x is not a T
3 of 7
5/25/2012 11:41 AM
http://msdn.microsoft.com/en-us/library/ms173145
Expression
x == y x != y Logical, Conditional, and Null Operators
Description
Equal Not equal
Category
Logical AND Logical XOR Logical OR Conditional AND Conditional OR Null coalescing Conditional
Expression
x&y x^y x|y x && y x || y x ?? y x?y:z
Description
Integer bitwise AND, Boolean logical AND Integer bitwise XOR, boolean logical XOR Integer bitwise OR, boolean logical OR Evaluates y only if x is true Evaluates y only if x is false Evaluates to y if x is null, to x otherwise Evaluates to y if x is true, z if x is false
Expression Description
= x op= y Assignment Compound assignment. Supports these operators: +=, -=, *=, /=, %=, &=, |=, !=, <<=, >>= Anonymous function (lambda expression)
(T x) => y
Associativity
When two or more operators that have 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. For example, the assignment operator is right associative. If it were not, the following code would result in an error.
4 of 7
5/25/2012 11:41 AM
http://msdn.microsoft.com/en-us/library/ms173145
int a, b, c; c = 1; // The following two lines are equivalent. a = b = c; a = (b = c); // The following line, which forces left associativity, causes an error. //(a = b) = c; The assignment operators and the ternary operator (?:) are right associative. All other binary operators are left associative. Whether the operators in an expression are left associative or right associative, the operands of each expression are evaluated first, from left to right. The following examples illustrate the order of evaluation of operators and operands.
Statement
a = b a = b + c a = b + c * d a = b * c + d a = b - c + d a += b -= c
Order of evaluation
a, b, = a, b, c, +, = a, b, c, d, *, +, = a, b, c, *, d, +, = a, b, c, -, d, +, = a, b, c, -=, +=
Adding Parentheses
You can change the order imposed by operator precedence and associativity by using parentheses. For example, 2 + 3 * 2 ordinarily evaluates to 8, because multiplicative operators take precedence over additive operators. However, if you write the expression as (2 + 3) * 2, the addition is evaluated before the multiplication, and the result is 10. The following examples illustrate the order of evaluation in parenthesized expressions. As in previous examples, the operands are evaluated before the operator is applied.
Statement
a = (b + c) * d a = b - (c + d) a = (b + c) * (d - e)
Order of evaluation
a, b, c, +, d, *, = a, b, c, d, +, -, = a, b, c, +, d, e, -, *, =
5 of 7
5/25/2012 11:41 AM
http://msdn.microsoft.com/en-us/library/ms173145
Operator Overloading
You can change the behavior of operators for custom classes and structs. This process is referred to as operator overloading. For more information, see Overloadable Operators (C# Programming Guide).
Related Sections
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
Yes
No
Community Content
Typo
Under (Logical, Conditional, and Null Operators) and category (Conditional), the expression (x ?: y : z) needs to be corrected to (x ? y : z).
Edit by SJ at MSFT: Thanks. I have fixed this. The update should appear on msdn soon. 1/11/2012 SJ at MSFT 12/15/2011 Dhruv Rangunwala
Associativity Correction
This page and the C# 4.0 spec are both incorrect (out-of-date) when they say that "all other binary operators are left associative" other than all assignment operators and the conditional operator. The null coalescing (or if-null-then) operator is a binary operator, but it's also RIGHT associative. 10/26/2011 Ken Beckett
6 of 7
5/25/2012 11:41 AM
http://msdn.microsoft.com/en-us/library/ms173145
7 of 7
5/25/2012 11:41 AM