Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 05 Beginner

Operators in C#

Updated: May 17, 2026
5 min read

# CHAPTER 5

Operators in C#

1. Introduction

Operators are the action verbs of a programming language. They take variables and values, perform operations on them, and produce results. Just as a calculator uses + and -, C# provides a rich set of operators for mathematics, logic, and data manipulation.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use Arithmetic operators for calculations.
  • Use Comparison and Logical operators for decision-making.
  • Use Assignment operators to update variables.
  • Understand the Null-coalescing operator (??).
  • Use the Ternary operator (?:) as a shorthand.

3. Arithmetic Operators

Used for standard mathematical operations.
OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus (Remainder)10 % 31
++Incrementx++Increases x by 1
--Decrementx--Decreases x by 1

Important Note on Division: If you divide two integers, C# truncates the decimal. 5 / 2 results in 2. To get 2.5, at least one number must be a double: 5.0 / 2.

4. Assignment Operators

Used to assign values to variables.
OperatorExampleEquivalent to
=x = 5Assigns 5 to x
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3

5. Comparison Operators

Used to compare two values. They always return a bool (true or false).
OperatorMeaningExampleResult
==Equal to5 == 3false
!=Not equal5 != 3true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater or equal5 >= 5true
<=Less or equal5 <= 3false

6. Logical Operators

Used to combine multiple comparison conditions.
OperatorNameDescriptionExample
&&Logical ANDTrue ONLY if BOTH are true(5 > 3) && (2 < 4) is true
||Logical ORTrue if AT LEAST ONE is true(5 < 3) || (2 < 4) is true
!Logical NOTReverses the result!(5 == 5) is false

7. The Ternary Operator (?:)

A one-line shorthand for a simple if-else statement. Syntax: condition ? valueIfTrue : valueIfFalse;
csharp
123
int age = 20;
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(status); // Prints "Adult"

8. The Null-Coalescing Operator (??)

A very powerful operator in C#. It returns the left-hand operand if it is NOT null; otherwise, it returns the right-hand operand.
csharp
1234567
string defaultName = "Guest";
string userName = null; // Imagine this came from an empty database field

// If userName is null, use defaultName instead
string displayName = userName ?? defaultName;

Console.WriteLine(displayName); // Prints "Guest"

*(Note: C# 8.0 introduced the ??= operator, which assigns the right value to the left variable ONLY if the left variable is currently null).*

9. Common Mistakes

  • Confusing = and ==: x = 5 assigns the value 5 to x. x == 5 checks if x is equal to 5.
  • Modulus on Doubles: The % operator is almost exclusively used with integers (e.g., checking if a number is even by doing x % 2 == 0).

10. Best Practices

  • Always use parentheses () when combining multiple logical operators to make the order of operations explicitly clear. Example: if ((age > 18) && (hasLicense == true))

11. Exercises

  1. 1. Write a program that calculates the area and perimeter of a rectangle (Area = width * height, Perimeter = 2 * (width + height)).
  1. 2. Create an integer variable points = 100. Use the += operator to add 50 points, then print the result.
  1. 3. Use the ternary operator to check if a number is even or odd, and print the result.

12. MCQs with Answers

Question 1

What is the result of 10 % 4?

Question 2

Which operator checks if two values are equal?

Question 3

What does x += 5 mean?

Question 4

If a = true and b = false, what is a && b?

Question 5

What is the Logical OR operator?

Question 6

What does !true return?

Question 7

How does C# evaluate double x = 5 / 2;?

Question 8

What does the Ternary operator ?: do?

Question 9

What does the Null-coalescing operator ?? do?

Question 10

What is the output of int x = 5; x++; Console.Write(x);?

13. Interview Questions

  • Q: Explain short-circuit evaluation in C# logical operators (&& and ||).
  • Q: What is the difference between i++ (postfix) and ++i (prefix)?
  • Q: Describe a real-world scenario where the null-coalescing operator (??) is highly beneficial.

14. Summary

Operators are the backbone of program logic. Arithmetic operators handle math, relational operators compare values, and logical operators combine conditions. The ternary and null-coalescing operators are C# shortcuts that make your code cleaner and more professional.

15. Next Chapter Recommendation

In Chapter 6: User Input and Output, we will make our programs interactive by learning how to ask the user for data using Console.ReadLine() and format output beautifully.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·