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#.
|operatorSymbol||The operator being overloaded, e.g. +, -, /, *|
|OperandType||The type that will be returned by the overloaded operator.|
|operand1||The first operand to be used in performing the operation.|
|operand2||The second operand to be used in performing the operation, when doing binary operations.|
|statements||Optional code needed to perform the operation before returning the result.|
All operators are defined as
static methods and they are not
virtual and they are not inherited.
All operators have a particular "precedence" depending on which group the operator falls in (operators of the same group have equal precedence). Meaning some operators will be applied before others. What follows is a list of groups (containing their respective operators) ordered by precedence (highest first):
a.b- Member access.
a?.b- Null conditional member access.
->- Pointer dereferencing combined with member access.
f(x)- Function invocation.
a?[x]- Null conditional indexer.
x++- Postfix increment.
x--- Postfix decrement.
new- Type instantiation.
default(T)- Returns the default initialized value of type
typeof- Returns the
Typeobject of the operand.
checked- Enables numeric overflow checking.
unchecked- Disables numeric overflow checking.
delegate- Declares and returns a delegate instance.
sizeof- Returns the size in bytes of the type operand.
-x- Numeric negation.
!x- Logical negation.
~x- Bitwise complement/declares destructors.
++x- Prefix increment.
--x- Prefix decrement.
(T)x- Type casting.
await- Awaits a
&x- Returns the address (pointer) of
*x- Pointer dereferencing.
x * y- Multiplication.
x / y- Division.
x % y- Modulus.
x + y- Addition.
x – y- Subtraction.
Bitwise Shift Operators
x << y- Shift bits left.
x >> y- Shift bits right.
x < y- Less than.
x > y- Greater than.
x <= y- Less than or equal to.
x >= y- Greater than or equal to.
is- Type compatibility.
as- Type conversion.
x == y- Equality.
x != y- Not equal.
Logical AND Operator
x & y- Logical/bitwise AND.
Logical XOR Operator
x ^ y- Logical/bitwise XOR.
Logical OR Operator
x | y- Logical/bitwise OR.
Conditional AND Operator
x && y- Short-circuiting logical AND.
Conditional OR Operator
x || y- Short-circuiting logical OR.
x ?? y- Returns
xif it is not null; otherwise, returns
x ? y : z- Evaluates/returns
xis true; otherwise, evaluates