C# Operators
In C#, operators are special symbols that perform operations on one or more operands (variables or values). Operators can be categorized based on the type of operation they perform. Here’s a detailed explanation of the different types of operators in C#:
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Example:
int a = 10;
int b = 3;
int sum = a + b; // sum = 13
int difference = a - b; // difference = 7
int product = a * b; // product = 30
int quotient = a / b; // quotient = 3
int remainder = a % b; // remainder = 1
2. Relational Operators
Relational operators are used to compare two operands and return a boolean value (true
or false
).
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Example:
int a = 5;
int b = 10;
bool isEqual = (a == b); // isEqual = false
bool isGreater = (a > b); // isGreater = false
bool isLessOrEqual = (a <= b); // isLessOrEqual = true
3. Logical Operators
Logical operators are used to perform logical operations, often with boolean values.
Operator | Description | Example |
---|---|---|
&& | Logical AND | (a > b) && (c < d) |
` | ` | |
! | Logical NOT | !(a > b) |
Example:
bool condition1 = true;
bool condition2 = false;
bool resultAnd = condition1 && condition2; // resultAnd = false
bool resultOr = condition1 || condition2; // resultOr = true
bool resultNot = !condition1; // resultNot = false
4. Bitwise Operators
Bitwise operators perform operations on individual bits of integral types.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << 1 |
>> | Right shift | a >> 1 |
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
int resultAnd = a & b; // resultAnd = 1 (0001)
int resultOr = a | b; // resultOr = 7 (0111)
int resultXor = a ^ b; // resultXor = 6 (0110)
int resultNot = ~a; // resultNot = -6 (inverts bits)
int leftShift = a << 1; // leftShift = 10 (1010)
int rightShift = a >> 1; // rightShift = 2 (0010)
5. Assignment Operators
Assignment operators are used to assign values to variables. They can also perform operations while assigning values.
Operator | Description | Example |
---|---|---|
= | Assignment | a = b |
+= | Add and assign | a += b (equivalent to a = a + b ) |
-= | Subtract and assign | a -= b |
*= | Multiply and assign | a *= b |
/= | Divide and assign | a /= b |
%= | Modulus and assign | a %= b |
&= | Bitwise AND and assign | a &= b |
` | =` | Bitwise OR and assign |
^= | Bitwise XOR and assign | a ^= b |
<<= | Left shift and assign | a <<= b |
>>= | Right shift and assign | a >>= b |
Example:
int a = 5;
a += 3; // a = 8
a -= 2; // a = 6
6. Unary Operators
Unary operators operate on a single operand.
Operator | Description | Example |
---|---|---|
++ | Increment | ++a or a++ |
-- | Decrement | --a or a-- |
+ | Unary plus (no effect) | +a |
- | Unary negation | -a |
! | Logical negation | !a |
Example:
int a = 5;
a++; // a = 6 (post-increment)
++a; // a = 7 (pre-increment)
a--; // a = 6 (post-decrement)
--a; // a = 5 (pre-decrement)
7. Ternary Operator
The ternary operator is a shorthand for the if-else
statement. It has the following syntax:
condition ? expressionIfTrue : expressionIfFalse;
Example:
int a = 10;
int b = 20;
int max = (a > b) ? a : b; // max = 20
8. Null-Coalescing Operator
The null-coalescing operator (??
) is used to define a default value when a nullable type or reference type is null.
Example:
string name = null;
string defaultName = name ?? "Default Name"; // defaultName = "Default Name"
9. Type Comparison Operator
The is
operator is used to check if an object is compatible with a given type. The as
operator attempts to cast an object to a specified type, returning null if the cast fails.
Example:
object obj = "Hello, World!";
if (obj is string str) // Check type and cast
{
Console.WriteLine(str); // Output: Hello, World!
}
string anotherStr = obj as string; // Attempt to cast
if (anotherStr != null)
{
Console.WriteLine(anotherStr); // Output: Hello, World!
}
Summary
Operators in C# allow developers to perform a wide range of operations on variables and values. Here’s a recap of the main types of operators:
- Arithmetic Operators: For mathematical calculations.
- Relational Operators: For comparisons.
- Logical Operators: For boolean logic.
- Bitwise Operators: For operations on bits.
- Assignment Operators: For assigning values.
- Unary Operators: For operations on a single operand.
- Ternary Operator: For conditional assignments.
- Null-Coalescing Operator: For handling null values.
- Type Comparison Operator: For checking types and casting.
Understanding these operators is essential for effective programming in C#, enabling you to write efficient and functional code.