What are the Different Types of Operators in C#?
๐ก Concept: Operators in C#
Operators in C# are special symbols used to perform operations on operands such as variables and values. They form the foundation of expressions and logic in programming.
๐ Quick Intro
C# provides several categories of operators including arithmetic, logical, relational, bitwise, and assignment operators. Each serves a unique purpose in computation or program control.
๐ง Analogy
Think of operators as tools in a toolbox. Just as a screwdriver tightens screws and a hammer drives nails, C# operators perform specific actions like adding numbers or comparing values.
๐ง Technical Explanation
- Arithmetic operators: +, -, *, /, %
- Relational operators: ==, !=, >, <, >=, <=
- Logical operators: &&, ||, !
- Assignment operators: =, +=, -=, *=, /=
- Bitwise operators: &, |, ^, ~, <<, >>
- Unary operators: +, -, ++, --, !
- Conditional operator: ?:
- Null-coalescing operator: ??
๐ฏ Use Cases
- Arithmetic operators are used for calculations.
- Logical operators are used in decision-making and conditional flows.
- Bitwise operators are useful in system-level or performance-critical applications.
- Assignment operators help shorten code and make it readable.
๐ป Real Code Example
int a = 10, b = 5;
int sum = a + b; // Arithmetic
bool result = a > b; // Relational
bool flag = (a > 0) && (b > 0); // Logical
a += 2; // Assignment
int bit = a & b; // Bitwise
string name = null;
string finalName = name ?? "Default"; // Null-coalescing

โ Interview Q&A
Q1: What are the main types of operators in C#?
A: Arithmetic, relational, logical, assignment, bitwise, and unary operators.
Q2: What is the purpose of the null-coalescing operator?
A: It returns the left-hand operand if not null; otherwise the right-hand operand.
Q3: What's the difference between == and =?
A: == compares values; = assigns values.
Q4: How do logical operators work in C#?
A: They evaluate Boolean expressions and return true or false.
Q5: What is the purpose of bitwise operators?
A: They perform operations on bits and are used in low-level programming.
Q6: Can operators be overloaded in C#?
A: Yes, C# allows operator overloading using special syntax.
Q7: What does the ternary operator do?
A: It provides a shorthand for if-else conditions: condition ? trueResult : falseResult.
Q8: Is there a difference between ++a and a++?
A: Yes, ++a increments first, a++ returns the value before increment.
Q9: What is the purpose of &= or |=?
A: These are compound assignment operators combining bitwise and assignment.
Q10: What operator is used to determine type in C#?
A: The `is` operator checks object type compatibility.
๐ MCQs
Q1. Which operator is used for assignment?
- ==
- !=
- =
- ->
Q2. What does '==' operator do?
- Assigns values
- Performs addition
- Compares equality
- Checks type
Q3. Which is a logical operator?
- &
- ++
- &&
- |||
Q4. What is the result of 5 % 2 in C#?
- 2
- 0
- 1
- 5
Q5. What does '??' operator represent?
- Ternary
- Bitwise OR
- Null-coalescing
- Pointer
Q6. Which operator checks inequality?
- ==
- !=
- ||
- &&
Q7. Which of the following is a unary operator?
- +
- *
- =
- ++
Q8. What does '&' represent in bitwise context?
- OR operation
- XOR operation
- AND operation
- Left shift
Q9. Which operator returns true if both conditions are true?
- ||
- &&
- !
- ==
Q10. What is the result of 'true || false'?
- false
- true
- error
- null
๐ก Bonus Insight
Mastering operators lets you write cleaner logic, build conditions more concisely, and optimize your performance-critical code. Operator precedence and associativity also play a major role in expression evaluation.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!