Operators

Operators perform various operations on values, creating a new value. Depending on the number of operands, a distinction is made between unary, binary and ternary operators. The order of the evaluation is determined by the priority and associativity and can be changed by parentheses.

C# provides many operators, which are symbols that specify which operations (math, indexing, function call, etc.) to perform in an expression. You can overload many operators to change their meaning when applied to a user-defined type.

Operations on integral types (such as ==, !=, <, >, &, |) are generally allowed on enumeration (enum) types.

The table below list the C# operators starting with the highest precedence to the lowest. The operators within each section share the same precedence level (see also: docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators)

Operator precedence Operator Description
  1. Primary operators
x.y Member access
f(x) Aufrufen von Methoden und Delegaten
a[x] Aggregate object indexing
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).
new T(...) Type instantiation
new T(...){...} Type instantiation with initializers
new {...} Create instances of anonymous types
new T[...] Array creation
typeof(T) Retrieve the System.Type object for T
checked(x) Enables overflow checking for integer operations.
unchecked(x) Disables overflow checking for integer operations. This is the default compiler behavior.
default (T) Produces the default value of type T.
delegate {} Anonymous Method
  1. Unary operators
+x Returns the value of x (identity)
-x Numeric negation
!x Logical negation
~x Bitwise complement
++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 subtracts the integer 1).
(T)x Type casting
  1. Multiplicative operators
* Multiplication
/ Division. If the operands are integers, the result is an integer truncated toward zero (for example, -7/2 is -3).
% Remainder. 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.
  1. Additive operators
x + y Addition, string concatenation, delegate combination
x - y Subtraction, delegate distance
  1. Shift operators
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.
  1. Relational and type-testing operators
x < y Less than (true if x is less than y).
x > y Greater than (true if x is greater than y).
x <= y Less than or equal to.
x >= y Greater than or equal to.
x is T Type compatibility. Returns true if the evaluated left operand can be cast to the type specified in the right operand (a static type).
x as T 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.
  1. Equality operators
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 !=.
  1. Logical, conditional, and Null operators
x & y Logical AND for the bool operands or bitwise logical AND for the operands of the integral types.
x ^ y Logical XOR for the bool operands or bitwise logical XOR for the operands of the integral types.
x | y Logical OR for the bool operands or bitwise logical OR for the operands of the integral types.
x && y Logical AND. If the first operand evaluates to false, then C# does not evaluate the second operand.
x || y Logical OR. If the first operand evaluates to true, then C# does not evaluate the second operand.
x ?? y Returns x if it is non-null; otherwise, returns y.
x ? y : z If test t evaluates to true, then evaluate and return x; otherwise, evaluate and return y.
  1. Assignment and Lambda operators
= Assignment
x op= y

Compound assignment. Corresponds to x = x op y, where op is the operator +, -, *, /, %, &, |, ^, <<, >>.

(T x) => y Anonymous method (Lambda-declaration)

[Quelle: Wikipedia, docs.microsoft.com]