100% found this document useful (1 vote)
100 views

Operators (C# Programming Guide)

An operator is a program element that is applied to one or more operands in an expression or statement. One operator, the conditional operator (?:), takes three operands and is the sole ternary operator In C#. An operator can be a valid expression of any size, and it can be composed of any number of other expressions.

Uploaded by

Kyle Daly
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
100 views

Operators (C# Programming Guide)

An operator is a program element that is applied to one or more operands in an expression or statement. One operator, the conditional operator (?:), takes three operands and is the sole ternary operator In C#. An operator can be a valid expression of any size, and it can be composed of any number of other expressions.

Uploaded by

Kyle Daly
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Operators (C# Programming Guide)

http://msdn.microsoft.com/en-us/library/ms173145

Operators (C# Programming Guide)


Visual Studio 2010 6 out of 8 rated this helpful - Rate this topic In C#, an operator is a program element that is applied to one or more operands in an expression or statement. Operators that take one operand, such as the increment operator (++) or new, are referred to as unary operators. Operators that take two operands, such as arithmetic operators (+,-,*,/), are referred to as binary operators. One operator, the conditional operator (?:), takes three operands and is the sole ternary 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 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;

Operators, Evaluation, and Operator Precedence


An operand can be a valid expression of any size, and it can be composed of any number of other expressions. In an expression that contains multiple operators, the order in which the operators are applied is determined by operator precedence, associativity, and parentheses. Each operator has a defined precedence. In an expression that contains multiple operators that have different precedence levels, the precedence of the operators determines the order in which the operators are evaluated. For example, the following statement assigns 3 to n1.

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

Operators (C# Programming Guide)

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

Operators (C# Programming Guide)

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

Operators (C# Programming Guide)

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

Assignment and Anonymous Operators

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

Operators (C# Programming Guide)

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

Operators (C# Programming Guide)

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

Did you find this helpful?

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

Operators (C# Programming Guide)

http://msdn.microsoft.com/en-us/library/ms173145

2012 Microsoft. All rights reserved.

7 of 7

5/25/2012 11:41 AM

You might also like