Primary Operators: X.Y X?.y X? (Y) F (X) A (X) X++
Primary Operators: X.Y X?.y X? (Y) F (X) A (X) X++
Primary Operators: X.Y X?.y X? (Y) F (X) A (X) X++
Primary Operators
These are the highest precedence operators. NOTE, you can click on the operators to go the
detailed pages with examples.
x?.y – null conditional member access. Returns null if the left-hand operand is null.
x?[y] - null conditional index access. Returns null if the left-hand operand is null.
x++ – postfix increment. Returns the value of x and then updates the storage location with the
value of x that is one greater (typically adds the integer 1).
x-- – postfix decrement. Returns the value of x and then updates the storage location with the
value of x that is one less (typically subtracts the integer 1).
unchecked – disables overflow checking for integer operations. This is the default compiler
behavior.
default(T) – returns the default value of type T, null for reference types, zero for numeric types,
and zero/null filled in members for struct types.
Unary Operators
These operators have higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operators to go the detailed pages with examples.
++x – prefix increment. Returns the value of x after updating the storage location with the value
of x that is one greater (typically adds the integer 1).
--x – prefix decrement. Returns the value of x after updating the storage location with the value
of x that is one less (typically adds the integer 1).
*x – dereferencing.
Multiplicative Operators
These operators have higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operators to go the detailed pages with examples.
x * y – multiplication.
x / y – division. If the operands are integers, the result is an integer truncated toward zero (for
example, -7 / 2 is -3).
x % y – modulus. If the operands are integers, this returns the remainder of dividing x by y. If q
= x / y and r = x % y, then x = q * y + r.
Additive Operators
These operators have higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operators to go the detailed pages with examples.
x + y – addition.
x – y – subtraction.
Shift Operators
These operators have higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operators to go the detailed pages with examples.
x << y – shift bits left and fill with zero on the right.
x >> y – shift bits right. If the left operand is int or long, then left bits are filled with the sign bit.
If the left operand is uint or ulong, then left bits are filled with zero.
These operators have higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operators to go the detailed pages with examples.
is – type compatibility. Returns true if the evaluated left operand can be cast to the type specified
in the right operand (a static type).
as – type conversion. Returns the left operand cast to the type specified by the right operand (a
static type), but as returns null where (T)x would throw an exception.
Equality Operators
These operators have higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operators to go the detailed pages with examples.
x == y – equality. By default, for reference types other than string, this returns reference equality
(identity test). However, types can overload ==, so if your intent is to test identity, it is best to
use the ReferenceEquals method on object.
x != y – not equal. See comment for ==. If a type overloads ==, then it must overload !=.
x & y – logical or bitwise AND. Use with integer types and enum types is generally allowed.
This operator has higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operator to go the details page with examples.
x ^ y – logical or bitwise XOR. You can generally use this with integer types and enum types.
Logical OR Operator
This operator has higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operator to go the details page with examples.
x | y – logical or bitwise OR. Use with integer types and enum types is generally allowed.
This operator has higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operator to go the details page with examples.
x && y – logical AND. If the first operand is false, then C# does not evaluate the second
operand.
Conditional OR Operator
This operator has higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operator to go the details page with examples.
x || y – logical OR. If the first operand is true, then C# does not evaluate the second operand.
Null-coalescing Operator
This operator has higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operator to go the details page with examples.
Conditional Operator
This operator has higher precedence than the next section and lower precedence than the
previous section. NOTE, you can click on the operator to go the details page with examples.
t ? x : y – if test t is true, then evaluate and return x; otherwise, evaluate and return y.
These operators have higher precedence than the next section and lower precedence than the
previous section.
x = y – assignment.
x += y – increment. Add the value of y to the value of x, store the result in x, and return the new
value. If x designates an event, then y must be an appropriate function that C# adds as an event
handler.
x -= y – decrement. Subtract the value of y from the value of x, store the result in x, and return
the new value. If x designates an event, then y must be an appropriate function that C# removes
as an event handler
x *= y – multiplication assignment. Multiply the value of y to the value of x, store the result in x,
and return the new value.
x /= y – division assignment. Divide the value of x by the value of y, store the result in x, and
return the new value.
x %= y – modulus assignment. Divide the value of x by the value of y, store the remainder in x,
and return the new value.
x &= y – AND assignment. AND the value of y with the value of x, store the result in x, and
return the new value.
x |= y – OR assignment. OR the value of y with the value of x, store the result in x, and return the
new value.
x ^= y – XOR assignment. XOR the value of y with the value of x, store the result in x, and
return the new value.
x <<= y – left-shift assignment. Shift the value of x left by y places, store the result in x, and
return the new value.
x >>= y – right-shift assignment. Shift the value of x right by y places, store the result in x, and
return the new value.
The arithmetic operators (+, -, *, /) can produce results that are outside the range of possible
values for the numeric type involved. You should refer to the section on a particular operator for
details, but in general:
When integer overflow occurs, what happens depends on the execution context, which can
be checked or unchecked. In a checked context, an OverflowException is thrown. In an
unchecked context, the most significant bits of the result are discarded and execution
continues. Thus, C# gives you the choice of handling or ignoring overflow. By default,
arithmetic operations occur in an unchecked context.
Relational operators
using System;
class Program {
int a = 21;
int b = 10;
if (a == b) {
} else {
Console.WriteLine("Line 1 - a is not equal to b");
if (a < b) {
} else {
if (a > b) {
} else {
a = 5;
b = 20;
if (a <= b) {
if (b >= a) {
}
Logical operators
using System;
namespace OperatorsAppl {
class Program {
bool a = true;
bool b = true;
if (a && b) {
if (a || b) {
a = false;
b = true;
if (a && b) {
} else {
}
if (!(a && b)) {
Console.ReadLine();
Bitwise operator
using System;
namespace OperatorsAppl {
class Program {
int c = 0;
c = a | b; /* 61 = 0011 1101 */
c = a ^ b; /* 49 = 0011 0001 */
Console.ReadLine();