C# Operators
Operators are special symbols or keywords that perform operations on one or more operands. C# provides a rich set of operators that can be broadly categorized as follows:
Arithmetic Operators
These operators are used for performing mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ |
Addition | int sum = a + b; |
- |
Subtraction | int difference = a - b; |
* |
Multiplication | int product = a * b; |
/ |
Division | int quotient = a / b; |
% |
Modulo (Remainder) | int remainder = a % b; |
++ |
Increment (adds 1) | x++; or ++x; |
-- |
Decrement (subtracts 1) | x--; or --x; |
Relational Operators
These operators are used to compare two values and return a boolean result (true or false).
| Operator | Description | Example |
|---|---|---|
== |
Equal to | if (a == b) |
!= |
Not equal to | if (a != b) |
> |
Greater than | if (a > b) |
< |
Less than | if (a < b) |
>= |
Greater than or equal to | if (a >= b) |
<= |
Less than or equal to | if (a <= b) |
Logical Operators
These operators are used to combine or modify boolean expressions.
| Operator | Description | Example |
|---|---|---|
&& |
Logical AND (short-circuiting) | if (condition1 && condition2) |
|| |
Logical OR (short-circuiting) | if (condition1 || condition2) |
! |
Logical NOT | if (!condition) |
& |
Bitwise AND (also logical AND if operands are bool) | int result = x & y; |
| |
Bitwise OR (also logical OR if operands are bool) | int result = x | y; |
^ |
Bitwise XOR | int result = x ^ y; |
?? |
Null-coalescing operator | string name = nullableName ?? "Default"; |
Assignment Operators
These operators are used to assign values to variables. They can be combined with arithmetic operators for shorthand.
| Operator | Description | Equivalent |
|---|---|---|
= |
Assignment | x = 5; |
+= |
Add and assign | x += 2; is equivalent to x = x + 2; |
-= |
Subtract and assign | x -= 2; is equivalent to x = x - 2; |
*= |
Multiply and assign | x *= 2; is equivalent to x = x * 2; |
/= |
Divide and assign | x /= 2; is equivalent to x = x / 2; |
%= |
Modulo and assign | x %= 2; is equivalent to x = x % 2; |
&= |
Bitwise AND and assign | x &= y; is equivalent to x = x & y; |
|= |
Bitwise OR and assign | x |= y; is equivalent to x = x | y; |
^= |
Bitwise XOR and assign | x ^= y; is equivalent to x = x ^ y; |
<<= |
Left shift and assign | x <<= 2; is equivalent to x = x << 2; |
>>= |
Right shift and assign | x >>= 2; is equivalent to x = x >> 2; |
Bitwise and Shift Operators
These operators work on the binary representation of integral types.
| Operator | Description | Example |
|---|---|---|
& |
Bitwise AND | byte result = x & y; |
| |
Bitwise OR | byte result = x | y; |
^ |
Bitwise XOR | byte result = x ^ y; |
~ |
Bitwise Complement (NOT) | int inverted = ~x; |
<< |
Left Shift | int shifted = x << 2; |
>> |
Right Shift | int shifted = x >> 2; |
Other Operators
C# includes several other useful operators:
.(Dot Operator): Used to access members of a class or struct (fields, methods, properties, etc.).
myObject.MyMethod();()(Parentheses): Used for method invocation and specifying order of operations.
result = (a + b) * c;[](Index Operator): Used to access elements of arrays and collections.
int element = myArray[index];new: Used to create an instance of a type.
MyClass obj = new MyClass();?:(Ternary Conditional Operator): A shorthand for an if-else statement that returns a value.
string status = (age >= 18) ? "Adult" : "Minor";is: Used to check if an object is compatible with a given type.
if (obj is string)as: Used for safe casting, returnsnullif the cast fails.
string str = obj as string;sizeof: Returns the size, in bytes, of a value type.
int size = sizeof(int);typeof: Returns theSystem.Typeobject for a given type.
Type intType = typeof(int);checkedandunchecked: Used to control overflow-checking behavior for arithmetic operations.=>(Expression-bodied members): A concise syntax for members that contain a single expression.
public int Square() => x * x;
Operator Precedence and Associativity
Operators have a precedence level that determines the order in which they are evaluated. For operators with the same precedence, associativity (left-to-right or right-to-left) determines the order. Parentheses can be used to override default precedence.
Understanding operators is fundamental to writing effective C# code. For a comprehensive list and detailed explanations, refer to the official C# Operator Reference.