These operators perform basic mathematical operations.
Example:
int a = 10, b = 5;
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; // Modulus
Relational Operators:
These operators compare values and return a boolean result.
Example:
int x = 10, y = 20;
bool isEqual = (x == y); // Equal to
bool isNotEqual = (x != y); // Not equal to
bool isGreaterThan = (x > y); // Greater than
bool isLessThan = (x < y); // Less than
Logical Operators:
These operators perform logical operations on boolean values.
Example:
bool p = true, q = false;
bool andResult = p && q; // Logical AND
bool orResult = p || q; // Logical OR
bool notResult = !p; // Logical NOT
Bitwise Operators:
These operators work on individual bits of integer types.
Example:
int num1 = 5, num2 = 3;
int andResult = num1 & num2; // Bitwise AND
int orResult = num1 | num2; // Bitwise OR
int xorResult = num1 ^ num2; // Bitwise XOR
int leftShiftResult = num1 << 2; // Left shift
int rightShiftResult = num1 >> 1; // Right shift
Assignment Operators:
These operators are used to assign values to variables.
Example:
int x = 10;
x += 5; // Equivalent to x = x + 5
x -= 3; // Equivalent to x = x - 3
Unary Operators:
These operators operate on a single operand.
Example:
int num = 5;
num++; // Increment by 1
num--; // Decrement by 1
Ternary Operator:
This is a conditional operator that allows for concise if-else statements.
Example:
int age = 20;
string message = (age >= 18) ? "Adult" : "Minor";
Misc Operators:
Various other operators in C# serve specific purposes, such as the member access operator (.), conditional access operator (?.), and null-coalescing operator (??).
Example:
string name = person?.Name; // Conditional Access Operator
int result = value ?? defaultValue; // Null-Coalescing Operator