Operators (Cont.)


Miscellaneous Operators
Few other important operators including sizeof, typeof, and ? : are supported by C#:

Operator Description Example
sizeof() Returns the size of a data type. sizeof(int), returns 4.
typeof() Returns the type of a class. typeof(StreamReader);
& Returns the address of an variable. &a; returns actual address of the variable.
* Pointer to a variable. *a; creates pointer named 'a' to a variable.
? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y
is Determines whether an object is of a certain type. If ( Ford is Car)
// Checks if Ford is an object of the Car class.
as Cast without raising an exception if the cast fails. Object obj = new StringReader("Hello");
StringReader r = obj as StringReader;

Operator Precedence
Operator precedence determines the grouping of terms in an expression. This affects evaluation of an expression. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators are evaluated first.

Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right