C# operators and their precedence closely resemble the operators in other languages of the C family.
Similar to C++, classes can overload most operators, defining or redefining the behavior of the operators in contexts where the first argument of that operator is an instance of that class, but doing so is often discouraged for clarity.
The following logical operators operate on boolean or integral operands, as noted.
& evaluates both of its operands and returns the logical conjunction ("AND") of their results. If the operands are integral, the logical conjunction is performed bitwise.
&& operates on boolean operands only. It evaluates its first operand. If the result is false, it returns false. Otherwise, it evaluates and returns the results of the second operand. Note that, if evaluating the second operand would hypothetically have no side effects, the results are identical to the logical conjunction performed by the & operator. This is an example of Short Circuit Evaluation.
a | b
| evaluates both of its operands and returns the logical disjunction ("OR") of their results. If the operands are integral, the logical disjunction is performed bitwise.
a || b
|| operates on boolean operands only. It evaluates the first operand. If the result is true, it returns true. Otherwise, it evaluates and returns the results of the second operand. Note that, if evaluating the second operand would hypothetically have no side effects, the results are identical to the logical disjunction performed by the | operator. This is an example of Short Circuit Evaluation.
^ returns the exclusive or ("XOR") of their results. If the operands are integral, the exclusive or is performed bitwise.
! operates on a boolean operand only. It evaluates its operand and returns the negation ("NOT") of the result. That is, it returns true if a evaluates to false and it returns false if a evaluates to true.
~ operates on integral operands only. It evaluates its operand and returns the bitwise negation of the result. That is, ~a returns a value where each bit is the negation of the corresponding bit in the result of evaluating a.
<< evaluates its operands and returns the resulting first argument left-shifted by the number of bits specified by the second argument. It discards high-order bits that shift beyond the size of its first argument and sets new low-order bits to zero.
>> evaluates its operands and returns the resulting first argument right-shifted by the number of bits specified by the second argument. It discards low-order bits that are shifted beyond the size of its first argument and sets new high-order bits to the sign bit of the first argument, or to zero if the first argument is unsigned.
The binary relational operators ==, !=, <, >, <=, and >= are used for relational operations and for type comparisons.
a == b
ais equal tob
For arguments of value type, the operator == returns true, if its operands have the same value, false otherwise. For the string type, it returns true, if the strings' character sequences match. For other reference types (types derived from System.Object), however, a == b returns true only if a and b reference the same object.
a != b
ais not equal tob
The operator != returns the logical negation of the operator ==. Thus, it returns true, if a is not equal to b, and false, if they are equal.
a < b
ais less thanb
The operator < operates on integral types. It returns true, if a is less than b, false otherwise.
a > b
ais greater thanb
The operator > operates on integral types. It returns true, if a is greater than b, false otherwise.
a <= b
ais less than or equal tob
The operator <= operates on integral types. It returns true, if a is less than or equal to b, false otherwise.
a >= b
ais greater than or equal tob
The operator >= operates on integral types. It returns true, if a is greater than or equal to b, false otherwise.
The assignment operators are binary. The most basic is the operator =. Not surprisingly, it assigns the value (or reference) of its second argument to its first argument.
(More technically, the operator = requires for its first (left) argument an expression to which a value can be assigned (an l-value) and for its second (right) argument an expression that can be evaluated (an r-value). That requirement of an assignable expression to its left and a bound expression to its right is the origin of the terms l-value and r-value.)
The first argument of the assignment operator (=) is typically a variable. When that argument has a value type, the assignment operation changes the argument's underlying value. When the first argument is a reference type, the assignment operation changes the reference, so the first argument typically just refers to a different object, but the object that it originally referenced does not change (except that it may no longer be referenced and may thus be a candidate for garbage collection).
a = b
aequals (or set to) b
The operator = evaluates its second argument and then assigns the results to (the l-value indicated by) its first argument.
a = b = c
bset toc, and then aset tob
Equivalent to a = (b = c). When there are consecutive assignments, the right-most assignment is evaluated first, proceeding from right to left. In this example, both variables a and b have the value of c.
NOTE: Most C# developers agree that direct manipulation and use of pointers is not recommended in C#. The language has many built-in classes to allow you to do almost any operation you want. C# was built with memory-management in mind and the creation and use of pointers is greatly disruptive to this end. This speaks to the declaration of pointers and the use of pointer notation, not arrays. In fact, a program may only be compiled in "unsafe mode", if it uses pointers.
Indirection operator. Allows access the object being pointed.
Similar to the . operator. Allows access to members of classes and structs being pointed.
Used to index a pointer.
References the address of the pointer.
allocates memory on the stack.
Temporarily fixes a variable in order that its address may be found.
if a and b are strings, concatenates a and b. If any addend is null, the empty string is used instead. If one addend is a string and the other one is a non-string object, ToString() is called on that object before concatenation.
if a and b are delegates, performs delegate concatenation
if a is true, returns the value of b, otherwise c
if a is null, returns b, otherwise returns a
verbatim text, i.e., escape characters are ignored