Different Operators in C# Explained with Examples
Introduction: What are Operators in C#?
In C#, operators are special symbols or keywords that perform operations on operands like variables, constants, and expressions to produce a result.
They are essential for performing calculations, comparisons, logical evaluations, and more.
In this blog, we’ll explore different categories of operators in C# with simple examples!
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division.
List of Arithmetic Operators:
+
: Addition (adds two operands)-
: Subtraction (subtracts the second operand from the first)*
: Multiplication (multiplies two operands)/
: Division (divides the first operand by the second)%
: Modulus (returns the remainder of division)
Example:
int a = 5;
int b = 3;
int sum = a + b; // 8
int difference = a - b; // 2
int product = a * b; // 15
int quotient = a / b; // 1
int remainder = a % b; // 2
2. Assignment Operators
Assignment operators are used to assign values to variables.
List of Assignment Operators:
=
: Assigns the value of the right operand to the left operand.+=
,-=
,*=
,/=
,%=
: Compound assignment operators (perform an operation and assign the result).
Example:
int x = 10;
int y = 5;
x += y; // x = x + y = 15
Assignment operators are great for simplifying expressions and updating variable values efficiently.
3. Comparison Operators
Comparison operators are used to compare two operands.
They return a boolean value (true
or false
) based on the comparison result.
List of Comparison Operators:
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Example:
int p = 5;
int q = 3;
bool isEqual = p == q; // false
bool isGreaterThan = p > q; // true
These operators are essential in decision-making constructs like if
, while
, and for
loops.
4. Logical Operators
Logical operators are used to perform logical operations, especially in control flow and conditional statements.
List of Logical Operators:
&&
: Logical AND (returnstrue
if both operands aretrue
)||
: Logical OR (returnstrue
if at least one operand istrue
)!
: Logical NOT (reverses the logical state)
Example:
bool isTrue = true;
bool isFalse = false;
bool result = isTrue && isFalse; // false
Logical operators are mainly used to combine multiple conditions.
5. Increment and Decrement Operators
Increment (++
) and decrement (--
) operators are used to increase or decrease the value of a variable by one.
Example:
int count = 0;
count++; // count = 1
count--; // count = 0
These are especially useful in loops and counters.
6. Conditional (Ternary) Operator
The conditional operator (? :
) is a shorthand for if-else
statements.
It evaluates a condition and returns one of two values based on the condition's result.
Syntax:
(condition) ? trueExpression : falseExpression;
Example:
int x = 5;
int y = (x > 0) ? 10 : -10; // y = 10
The ternary operator helps simplify short conditional assignments.
Conclusion: Mastering Operators in C#
Operators in C# are fundamental building blocks that allow you to write expressive, clear, and powerful code.
By mastering different types of operators — arithmetic, assignment, comparison, logical, increment/decrement, and conditional — you’ll be able to perform operations efficiently and make better programming decisions.
Keep practicing these operators with different examples to deepen your understanding and become proficient in C# programming!
0 Comments