C# MASTER

Operators

Back to Table of Contents

Here is a list of the operators of the C# programming language. The Precedence indicates which operator will be acted upon first if two operators are in the same scope. To make a certain operator acted on before another, surround it with this operator () as in 4*(5+3) will evaluate to 32 while 4*5+3 will evaluate to 60. When two operators are in the same scope the operator with the least precedence will be done first. If the two operators have the same precedence, then the operator to act on first is chosen by Associativity (left to right or right to left).

The C# compiler ignores white space before and after operators so you can have an expression on multiple lines as in the following example:

String A = "This is one way to "+
	"make a long sentence in the C# language. ";

Operator Precedence Associativity Overloadable? Description
( ) 1 Left to Right No (Primary) Parentheses: Makes what is between the parantheses acted upon before any outside operators are acted upon. Do not mistake this operator for the Casting operator described below. Also used for invoking methods and delegates.
[ ] 1 Left to Right No for Array Indexes
Yes for Indexers
(Primary) Used with arrays, indexers, and attributes. An array is a series of objects of the same type which can be accessed like:
int[] myArray = new int[4]{1,2,3,4};
myArray[2] = 7;
int myInt = myArray[3];
    //multidimensional Rectangular array:
int[,] myMultiDimRectArray = new int[7, 3];
    //accessing a multidimensional Rectangular array: 
myMultiDemArray[5, 2] = myInt; 
    //multidimensional Jagged Array:
int[][] myJaggedMultiDimArray = new int[7][];
    //assigning arrays to jagged multidimensional array:
myJaggedMultiDimArray[0] = new int[2]{1,2};
myJaggedMultiDimArray[1] = myArray;
    //accessing a multidimensional Jagged array: 
myJaggedMultiDimArray[1][3] = myInt;
array indexes must always be integer types, and array indexes cannot be overloaded. However, a type can define an indexer, which allows you to access items contained in your type instance as if your type instance were an array, though it really is a single object, such as a list or hashtable object. Also, indexers support any type index, not just integers, and there may be multiple parameters.
class MyIntContainer{
   int[] MyInts = new int[]{1,2,3};
   public int this[int i]{
      get{ return MyInts[i]; }
      set{ MyInts[i] = value; }
   }
}
//A really simple Indexer demo:
MyIntContainer My = new MyIntContainer();
My[2] = 4;

. 1 Left to Right No (Primary) Called "dot", this operator allows access to an objects members. The useage is to put the object to the left of the dot operator and the name of the member to access to the right of the dot operator.
new 1 Right to Left No (Primary) Used to create objects, invoke constructors, and create instances of types.
MyType myObj = new MyType();
typeof 1 Right to Left No (Primary) Used to obtain the System.Type of any type, including user defined types. Example:
System.Type strType = typeof(string);
System.Reflection.MemberInfo[] strMembers = strType.GetMembers();
...
checked 1 Right to Left No (Primary) Used to designate code or a code block where integral arithmetic overflow will be checked. This is not the default. Can be nested in unchecked blocks.
unchecked 1 Right to Left No (Primary) Used to designate code or a code block where integral arithmetic overflow will not be checked. This is the default. Can be nested in checked blocks.
-> 1 Right to Left No (Primary) Used in unsafe code for dereferencing pointers and member access.
++ 1 for x++
2 for ++x
Right to Left Yes (Primary for x++ and Unary for ++x) Unary post and preincrement. making a number increase by one. If the operator occurs before the variable, the variable is increased by one before its value is used in the expression. If the operator occurs after the variable, the initial value is used and then the variable is increased by one.
-- 1 for x--
2 for --x
Right to Left Yes (Primary for x-- and Unary for --x) Unary post and predecrement. making a number decrease by one. If the operator occurs before the variable, the variable is decreased by one before its value is used in the expression. If the operator occurs after the variable, the initial value is used and then the variable is decreased by one.
+ 2 Right to Left Yes (Unary) Unary Plus: : Indicates Positive value for a literal number to its right. Do not confuse this operator with the the Addition Operator described below or the String Concatenation operator described below. By default a literal number is positive.
- 2 Right to Left Yes (Unary) Unary Minus: : Indicates a negative value for a literal number to its right and reverses the sign of the number in a variable to its right. Do not confuse this operator with the the Subtraction Operator described below
~ 2 Right to Left Yes (Unary) One's complement: Evaluates the number to the right to a new number in which all the bits are reversed (1 becomes 0 and 0 becomes 1) and if the number is a signed number, its sign bit is also reversed. The only unsigned number in the Java language is char.
! 2 Right to Left Yes (Unary) Boolean Complement. Reverses the boolean value of the expression to its right.
( ) 2 Right to Left Yes (Unary) Casting of Types: converts an object from one of its possible types to another. Possible types include all its interfaces, all its parent classes, and all the interfaces of all its parent classes. The object to cast is placed on the right of the operator and the class or interface to cast it to is placed inbetween the two parentheses of the casting operator. Do not mistake this operator for the Parentheses operator described above.
true 2 Right to Left Yes, but also needs false (Unary) Returns true if operand is true, and false if it is false. Used to control bool and conditional expressions. Originally intended to enable the creation of user defined nullable types. This operator is no longer necessary due to the inclusion of nullable types into the language as of C# 2.0.
false 2 Right to Left Yes, but also needs true (Unary) Returns true if operand is false, and false if it is true. Used to control bool and conditional expressions. Originally intended to enable the creation of user defined nullable types. This operator is no longer necessary due to the inclusion of nullable types into the language as of C# 2.0.
& 2 Right to Left No (Unary) Address of: Returns address in memory of operand on the right in unsafe code. Do not confuse this operator with the binary AND operator.
sizeof 2 Right to Left Yes (Unary) Returns the size in bytes of an unmanaged type, including the built in types, such as int, float, bool, and char, as well as Enum types, Pointer types, and structs that contain no reference types.
* 3 Left to Right Yes (Multiplicative) Multiplication: Multiplies the numbers on either side of this operator. If both numbers are integers it evaluates to an integer.
/ 3 Left to Right Yes (Multiplicative) Division: Divides the number on the left of this operator by the number on the right and returns the result. If both numbers are integers it evaluates to an integer, ignoring the remainder (see modulo)
% 3 Left to Right Yes (Multiplicative) Modulo: Evalutes to the remainder of integer division where the integer on the left of this operator is divided by the integer on the right.
+ 4 Left to Right Yes (Additive) Addition: Evalutates to the sum of the two numbers on either side of this operator. If the two numbers are both integers the result will be an integer, otherwise it will be a floating point number (float or double). Do not confuse this operator with the Unary Plus operator described above Overloaded to provide for String Concatenation. Concats One String to another. The Object on the left must be a String, but the expression on the right can be any object or primitive type. If the expression to the right is an Object the String added will be the derived from its ToString() method.
- 4 Left to Right Yes (Additive) Subtraction: Evalutates to the difference of the two numbers on either side of this operator by subtracting the value on the right from the value on the left. The expressions on both sides must evalute to a number (int, short, long, byte, char, float, or double). If the two numbers are both integers the result will be an integer, otherwise it will be a floating point number (float or double). Do not confuse this operator with the Unary Minus operator described above.
<< 5 Left to Right Yes (Shift) Bitwise Shift Left: shift bits in the number on the left of this operator a number of times to the left determined by the integer on the right of this operator. The bit to the far right is replaced by zero.
>> 5 Left to Right Yes (Shift) Bitwise Shift Right: shift bits in the number on the left of this operator a number of times to the right determined by the integer on the right of this operator. The bit to the far left is replaced by zero.
< 6 Left to Right Yes, but also needs > (Relational and Type Setting) Less Than: Evalutes to bool: true if the number value on the left is less than the value on the right.
> 6 Left to Right (Relational and Type Setting) Yes, but also needs < Greater Than: Evalutes to bool: true if the number value on the left is greater than the value on the right.
<= 6 Left to Right Yes, but also needs >= (Relational and Type Setting) Less Than or Equal To: Evalutes to bool: true if the number value on the left is less than or equal to the value on the right.
>= 6 Left to Right Yes, but also needs <= (Relational and Type Setting) Greater Than or Equal To: Evalutes to bool: true if the number value on the left is greater than or equal to the value on the right.
is 6 Left to Right No (Relational and Type Setting) Evaluates to bool: true if the Object on the left can be cast to the type on the right, essentially whether the Object on the left is an instance of the type on the right.
as 6 Left to Right No (Relational and Type Setting) Casts the object on the left to the type on the right, returning null if it fails, throwing no Exceptions.
== 7 Left to Right Yes, but also needs != (Equality) Equals: Evaluates to bool: for primitive types, true if the values on either side are equal. For Objects, true if the two Objects are the same instance. example: if (a == b) ...; To compare two different instaces for holding the same value, like two Strings, use the equals method. example: if (MyString.equals(YourString)) doSomething();
!= 7 Left to Right Yes, but also needs == (Equality) Not Equal: Evaluates to bool: for primitive types, true if the values on either side are not equal. For Objects, true if the two Objects are not the same instance. example: if (a != b) ...; To compare two different instaces for holding the same value, like two Strings, use the equals method with a ! before it. example: if (! MyString.equals(YourString)) doSomething();
& 8 Left to Right Yes (Logical AND) Boolean AND and Bitwise AND: Evaluates the bits of the two numbers on either side to make a new number in which each bit will be on only if both of the corresponding bits of both numbers is also on. Returns boolean if both expressions are boolean and a number if both expressions are numbers. Do not confuse this operator with the unary Address of operator used in unsafe code.
^ 9 Left to Right Yes (Logical XOR) Boolean XOR and Bitwise XOR: Evaluates the bits of the two numbers on either side to make a new number in which each bit will be on only if the two corresponding bits of both numbers are different. Returns boolean if both expressions are boolean and a number if both expressions are numbers.
| 10 Left to Right Yes (Logical OR) Boolean OR and Bitwise OR: Evaluates the bits of the two numbers on either side to make a new number in which each bit will be on only if at least one of two the corresponding bits of both numbers is on. Returns boolean if both expressions are boolean and a number if both expressions are numbers.
&& 11 Left to Right No, but uses & (Conditional AND) Conditional AND: Evaluates to bool: true if both boolean expressions on either side of this operator are true, otherwise it evaluates to false.
|| 12 Left to Right No, but uses | (Conditional OR) Conditional OR: Evaluates to bool: true if one or both of the boolean expressions on either side of this operator are true, otherwise it evaluates to false.
? : 13 Right to Left No (Conditional) Conditional Ternary Operator: Takes three values. If the boolean expression on the left of the question mark (?) is true, it evaluates to the expression between the question mark and the colon (:). Otherwise, it evaluates to the expression after the colon. Example:
public String NoNull(String S){
   return S == null ? "" : S;
}
= 14 Right to Left No (Assignment) Assignment: Assigns the value on the right to the variable on the left.
*= 14 Right to Left No, but uses * (Assignment) Assignment: Perfoms the Multiplication operation between the values on both sides and then assigns it to the variable on the left.
/= 14 Right to Left No, but uses / (Assignment) Assignment: Perfoms the Division operation between the values on both sides and then assigns it to the variable on the left.
%= 14 No, but uses % Right to Left (Assignment) Assignment: Perfoms the Modulo operation between the values on both sides and then assigns it to the variable on the left.
+= 14 No, but uses + Right to Left (Assignment) Assignment: Perfoms the Addition operation between the values on both sides and then assigns it to the variable on the left.
-= 14 Right to Left No, but uses - (Assignment) Assignment: Perfoms the Subtraction operation between the values on both sides and then assigns it to the variable on the left.
<<= 14 Right to Left No, but uses << (Assignment) Assignment: Perfoms the Bitwise Shift Left operation between the values on both sides and then assigns it to the variable on the left.
>>= 14 Right to Left No, but uses >> (Assignment) Assignment: Perfoms the Bitwise Shift Right operation between the values on both sides and then assigns it to the variable on the left.
&= 14 Right to Left No, but uses & (Assignment) Assignment: Perfoms the Bitwise or Boolean AND operation between the values on both sides and then assigns it to the variable on the left.
^= 14 Right to Left No, but uses ^ (Assignment) Assignment: Perfoms the Bitwise or Boolean XOR operation between the values on both sides and then assigns it to the variable on the left.
?? 14 Right to Left No (Null Coalescing) If the object or value on the left is not null, it evaluates to that object or value. Otherwise, if that object or value is null, it evaluates to the expression on the right. Used with nullable value types and reference types.
=> 14 Right to Left No (Lambda) Used in Lambda expressions separating input variables on the left from the body of the Lambda expression on the right.

Other Characters Used in the C# Language:

Symbol Description
{ Open Curly Brackets: Indicates the start of a code blocks such as for a class, a method, a loop, or other block of code. Also used to initialize a array:
int[] myarray = {1,2,3};
} Close Curly Brackets: Indicates the end of a code blocks such as for a class, a method, a loop, or other block of code. Also used to initialize a array:
int[] myarray = {1,2,3};
( Open Parenthesis: When not used as an operator, this is used to indicate the start of the parameter holder of both a method declaration and a method call.
) Close Parenthesis: When not used as an operator, this is used to indicate the end of the parameter holder of both a method declaration and a method call.
, Comma: Used to separate values in a list, as in separating the parameters in a method declaration or a method call, separating the base class name and interface names at the top of a class declaration, separating the initializaions in a single declaration of multiple variables of the same type, separating the multiple incrementations in a for loop declaration
(example: for (int i=0, j=0; i < 10 && j < 20; ++i, j+=2){},

and separating the values in a array declaration.
: Colon: Used in the Ternary Operator and also in the Switch Statement using the switch keyword and also in goto labels: example:
if (myvar == true){
   goto MyJump;
} 
... //code that gets skipped if myvar is true
MyJump:
... //code to execute after MyJump
; Semi Colon: All Statements must end with a semicolon. The shortest statement possible is a single semicolon on its own. Semicolon is also used in for loops to separate the initialization from the loop condition and the the loop condition from the increment section.
Example: for (int i=0; i<10; ++i){ ; }

*OracleTM and JavaTM are registered trademarks of Oracle and or its affiliates. Other names may be trademarks of their respective owners.*