Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on numeric operands. There are six arithmetic operators in C#: addition (+), subtraction (-), multiplication (*), division (/), modulus (%), and increment/decrement (++/--).
Example:
int x = 10, y = 5; int sum = x + y; int difference = x - y; int product = x * y; int quotient = x / y; int remainder = x % y; x++; y--;Comparison Operators
Comparison operators are used to compare the values of two operands. There are six comparison operators in C#: equality (==), inequality (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
Example:
int x = 10, y = 5; bool isEqual = x == y; bool isNotEqual = x != y; bool isGreaterThan = x > y; bool isLessThan = x < y; bool isGreaterOrEqual = x >= y; bool isLessOrEqual = x <= y;Logical Operators
Logical operators are used to perform logical operations on boolean operands. There are three logical operators in C#: AND (&&), OR (||), and NOT (!).
Example:
bool a = true, b = false, c = true; bool andResult = a && b && c; bool orResult = a || b || c; bool notResult = !a;Bitwise Operators
Bitwise operators are used to perform bit-level operations on operands. There are six bitwise operators in C#: bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise complement (~), left shift (<<), and right shift (>>).
Example:
int x = 5, y = 3; int bitwiseAnd = x & y; int bitwiseOr = x | y; int bitwiseXor = x ^ y; int bitwiseComplement = ~x; int leftShift = x << 1; int rightShift = x >> 1;Assignment Operators
Assignment operators are used to assign a value to a variable. There are several assignment operators in C#: simple assignment (=), addition assignment (+=), subtraction assignment (-=), multiplication assignment (*=), division assignment (/=), modulus assignment (%=), bitwise AND assignment (&=), bitwise OR assignment (|=), bitwise XOR assignment (^=), left shift assignment (<<=), and right shift assignment (>>=).
Example:
int x = 10, y = 5; x = y; // equivalent to x = y; x += y; // equivalent to x = x + y; x -= y; // equivalent to x = x - y; x *= y; // equivalent to x = x * y; x /= y; // equivalent to x = x / y; x %= y; // equivalent to x = x % y; x &= y; // equivalent to x = x & y; x |= y; // equivalent to x = x | y; x ^= y; // equivalent to x = x ^ y x <<= y; // equivalent to x = x << y; x >>= y; // equivalent to x = x >> yConditional Operator
Conditional Operator is C#: conditional operator or ternary operator (?:).
Example:
int x = 10, y = 5; var result = x > y ? "x is greater than y" : "x is less than y";Operator Precedence and Associativity
Category | Operator | Associativity |
---|---|---|
Postfix | () [] -> . ++ - - | Left to right |
Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | + - | Left to right |
Shift | << >> | Left to right |
Relational | < <= > >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
Operator Shortcuts
C# provides some shortcuts for common operations that can be used in assignment expressions. These shortcuts are shown in the following table:
Shortcut Operator | Equivalent Expression |
---|---|
x++ or ++x | x = x + 1 |
x-- or --x | x = x - 1 |
x += y | x = x + y |
x -= y | x = x - y |
x *= y | x = x * y |
x /= y | x = x / y |
x %= y | x = x % y |
x <<= y | x = x << y |
x >>= y | x = x >> y |
x &= y | x = x & y |
x |= y | x = x | y |
x ^= y | x = x ^ y |
Operators are an essential part of the C# programming language, and understanding them is crucial for writing effective code. In this tutorial, we have covered the various types of operators in C#, their precedence and associativity, and some common shortcuts. By applying this knowledge to your code, you can write more efficient and readable programs. Remember to pay attention to operator precedence and associativity, and you'll be on your way to becoming a C# expert!