Chapter 4: Operators
1. What Are Operators? (Super Simple Explanation)
Operators are special symbols that perform actions on one or more values (called operands).
Examples you already know from school:
- 5 + 3 → + is the operator
- 10 > 7 → > is the operator
In C#, there are many types of operators. We’ll learn the most important ones today.
2. Arithmetic Operators (Math Operations)
These are used for calculations – just like your calculator.
| Operator | Name | Example | Result | Notes |
|---|---|---|---|---|
| + | Addition | 5 + 3 | 8 | Also works with strings! |
| – | Subtraction | 10 – 4 | 6 | |
| * | Multiplication | 6 * 7 | 42 | |
| / | Division | 10 / 3 | 3 (int) or 3.333… (double) | Integer division truncates! |
| % | Modulus (Remainder) | 10 % 3 | 1 | Very useful for even/odd checks |
| + | String Concatenation | “Hello” + ” World” | “Hello World” |
Real code example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
var a = 10; var b = 3; Console.WriteLine($"Addition: {a} + {b} = {a + b}"); // 13 Console.WriteLine($"Subtraction: {a} - {b} = {a - b}"); // 7 Console.WriteLine($"Multiplication: {a} * {b} = {a * b}"); // 30 Console.WriteLine($"Division: {a} / {b} = {a / b}"); // 3 (integer division!) Console.WriteLine($"Division (double): {a} / (double){b} = {a / (double)b}"); // 3.333... Console.WriteLine($"Remainder: {a} % {b} = {a % b}"); // 1 // String example string first = "Webliance"; string last = " from Hyderabad"; Console.WriteLine(first + last); // "Webliance from Hyderabad" |
Quick Tip: If both operands are int → result is int (truncates decimals). Want decimal result? → cast at least one to double or decimal.
3. Assignment Operators (Giving Values)
The most common is = – but there are shorthand versions too!
| Operator | Meaning | Example | Same as |
|---|---|---|---|
| = | Assign | x = 5 | |
| += | Add and assign | x += 3 | x = x + 3 |
| -= | Subtract and assign | x -= 2 | x = x – 2 |
| *= | Multiply and assign | x *= 4 | x = x * 4 |
| /= | Divide and assign | x /= 2 | x = x / 2 |
| %= | Modulus and assign | x %= 3 | x = x % 3 |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var money = 1000; Console.WriteLine($"Initial: ₹{money}"); money += 500; // Got salary Console.WriteLine($"After salary: ₹{money}"); money -= 200; // Bought coffee Console.WriteLine($"After coffee: ₹{money}"); money *= 2; // Double or nothing! Console.WriteLine($"Doubled: ₹{money}"); |
4. Comparison Operators (Make Decisions)
These return true or false (bool).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| > | Greater than | 10 > 7 | true |
| < | Less than | 3 < 8 | true |
| >= | Greater than or equal | 5 >= 5 | true |
| <= | Less than or equal | 4 <= 6 | true |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
var age = 25; Console.WriteLine($"Is adult? {age >= 18}"); // true Console.WriteLine($"Is teenager? {age >= 13 && age <= 19}"); // false Console.WriteLine($"Is exactly 25? {age == 25}"); // true Console.WriteLine($"Not 30? {age != 30}"); // true |
5. Logical Operators (Combine Conditions)
| Operator | Name | Example | Meaning |
|---|---|---|---|
| && | AND | true && false | Both must be true |
| || | OR | true || false | At least one must be true |
| ! | NOT | !true | Reverses the value |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var age = 25; var hasLicense = true; bool canDrive = age >= 18 && hasLicense; Console.WriteLine($"Can drive? {canDrive}"); // true bool canEnterClub = age >= 21 || hasLicense; Console.WriteLine($"Can enter club? {canEnterClub}"); // true (because of OR) bool isNotMinor = ! (age < 18); Console.WriteLine($"Not a minor? {isNotMinor}"); // true |
6. Increment / Decrement Operators (Very Common!)
| Operator | Meaning | Example | Effect |
|---|---|---|---|
| ++ | Increment by 1 | x++ or ++x | x = x + 1 |
| — | Decrement by 1 | x– or –x | x = x – 1 |
Difference between prefix (++x) and postfix (x++):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
var count = 5; Console.WriteLine(count++); // Prints 5, THEN increases to 6 Console.WriteLine(count); // 6 count = 5; Console.WriteLine(++count); // Increases to 6, THEN prints 6 |
Common use: Loops (we’ll see later!)
7. Null-Coalescing Operators (Modern & Super Useful!)
These help when a value might be null (nothing).
| Operator | Name | Example | Meaning |
|---|---|---|---|
| ?? | Null-coalescing | name ?? “Guest” | Use left if not null, else use right |
| ??= | Null-coalescing assignment | name ??= “Guest” | Assign right only if left is null |
| ?. | Null-conditional | person?.Age | If person is null → whole expression is null |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
string username = null; string displayName = username ?? "Guest User"; Console.WriteLine(displayName); // "Guest User" string name = null; name ??= "Webliance"; // Only assigns if name was null Console.WriteLine(name); // "Webliance" var person = new { Name = "Webliance", Age = 25 }; // var person = null; // try this too! var age = person?.Age; // Safe! Won't crash if person is null Console.WriteLine(age ?? "Unknown"); // 25 or "Unknown" |
Mini-Project: Simple Shopping Cart
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Console.WriteLine("=== Shopping Cart ===\n"); var itemPrice = 599.99m; var quantity = 3; var discountPercent = 15; var subtotal = itemPrice * quantity; var discountAmount = subtotal * (discountPercent / 100m); var total = subtotal - discountAmount; Console.WriteLine($"Item price : ₹{itemPrice:N2}"); Console.WriteLine($"Quantity : {quantity}"); Console.WriteLine($"Subtotal : ₹{subtotal:N2}"); Console.WriteLine($"Discount ({discountPercent}%) : -₹{discountAmount:N2}"); Console.WriteLine($"------------------------"); Console.WriteLine($"Total : ₹{total:N2}"); Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); |
Your Homework (Fun & Practical!)
- Create a new console project called OperatorPlayground
- Declare variables for:
- Your current bank balance (decimal)
- Monthly salary (decimal)
- Number of coffees you drink per day (int)
- Price per coffee (decimal)
- Calculate:
- Total monthly coffee expense
- Remaining balance after salary + coffee expense
- Whether you can afford a new phone (price > remaining balance?)
- Use +=, %, ??, ?. at least once
Next lesson: Control Flow (if, else, switch, loops) – we’re going to make programs that make decisions and repeat actions!
You’re doing absolutely fantastic! 🎉 Any operator confusing you? Want more examples? Just ask — I’m right here for you! 💙
