Operators
## Learning Objectives
- Master arithmetic operators
- Understand comparison operators
- Learn logical operators
- Work with null coalescing operators
## Arithmetic Operators
### Basic Operations
```csharp
int a = 10, b = 3;
Console.WriteLine(a + b); // 13 (addition)
Console.WriteLine(a - b); // 7 (subtraction)
Console.WriteLine(a * b); // 30 (multiplication)
Console.WriteLine(a / b); // 3 (integer division)
Console.WriteLine(a % b); // 1 (modulus/remainder)
```
### Division Behavior
```csharp
// Integer division truncates
Console.WriteLine(10 / 3); // 3, not 3.333
// For floating-point
Console.WriteLine(10.0 / 3.0); // 3.333...
```
### Increment/Decrement
```csharp
int x = 5;
// Pre-increment/decrement
Console.WriteLine(++x); // 6 (increments first)
Console.WriteLine(--x); // 5 (decrements first)
// Post-increment/decrement
Console.WriteLine(x++); // 5 (uses value, then increments)
Console.WriteLine(x); // 6 (now incremented)
int y = 5;
Console.WriteLine(y--); // 5 (uses value, then decrements)
Console.WriteLine(y); // 4
```
## Comparison Operators
### Relational Operators
```csharp
int a = 5, b = 10;
Console.WriteLine(a == b); // False (equal)
Console.WriteLine(a != b); // True (not equal)
Console.WriteLine(a < b); // True (less than)
Console.WriteLine(a > b); // False (greater than)
Console.WriteLine(a <= b); // True (less or equal)
Console.WriteLine(a >= b); // False (greater or equal)
```
### Don't Confuse
```csharp
// = is assignment
// == is comparison
int x = 5;
// if (x = 10) // Error! Assignment returns 10 (truthy-ish)
// if (x == 5) // Correct! This compares
```
## Logical Operators
### AND, OR, NOT
```csharp
bool a = true, b = false;
Console.WriteLine(a && b); // False (AND - both must be true)
Console.WriteLine(a || b); // True (OR - one must be true)
Console.WriteLine(!a); // False (NOT - inverts)
```
### Short-Circuit Evaluation
```csharp
// && stops if first is false
if (x != 0 && y / x > 2) // Safe - won't divide by zero
// || stops if first is true
if (obj == null || obj.IsValid()) // Safe - won't call on null
```
### Truth Table
| A | B | A && B | A \|\| B | !A |
|---|---|--------|----------|-----|
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |
## Assignment Operators
### Simple Assignment
```csharp
int x = 10;
```
### Compound Assignment
```csharp
int x = 10;
x += 5; // x = 15 (x = x + 5)
x -= 3; // x = 12 (x = x - 3)
x *= 2; // x = 24 (x = x * 2)
x /= 4; // x = 6 (x = x / 4)
x %= 5; // x = 1 (x = x % 5)
```
## Null Coalescing Operators
### ?? (Null Coalescing)
```csharp
string name = null;
string displayName = name ?? "Unknown";
// Equivalent to:
string displayName2 = (name != null) ? name : "Unknown";
```
### ??= (Null Coalescing Assignment)
```csharp
string name = null;
name ??= "Unknown"; // Assigns only if null
// Now name is "Unknown"
```
### ?. (Null Conditional)
```csharp
string name = null;
int? length = name?.Length; // Returns null instead of throwing
// Safe navigation with null check
if (name?.Length > 0)
{
Console.WriteLine(name);
}
```
## Ternary Operator
### Syntax
```csharp
// condition ? valueIfTrue : valueIfFalse
int max = (a > b) ? a : b;
string status = (age >= 18) ? "adult" : "minor";
```
## Bitwise Operators
### Binary Operations
```csharp
int a = 5, b = 3; // 101 and 011 in binary
Console.WriteLine(a & b); // 1 (AND: 101 & 011 = 001)
Console.WriteLine(a | b); // 7 (OR: 101 | 011 = 111)
Console.WriteLine(a ^ b); // 6 (XOR: 101 ^ 011 = 110)
Console.WriteLine(~a); // -6 (NOT: ~101 = ...010 = -6)
Console.WriteLine(a << 1); // 10 (left shift: 101 << = 1010)
Console.WriteLine(a >> 1); // 2 (right shift: 101 >> = 10)
```
### Common Uses
```csharp
// Check if bit is set
int flags = 0b1010; // 10 in decimal
if ((flags & 0b1000) != 0)
{
Console.WriteLine("Bit 3 is set");
}
// Set a bit
flags = flags | 0b0001;
// Toggle a bit
flags = flags ^ 0b0100;
```
## Operator Precedence
### Highest to Lowest
| Priority | Operators |
|----------|-------------------|
| 1 | `()` |
| 2 | `++`, `--`, `!` |
| 3 | `*`, `/`, `%` |
| 4 | `+`, `-` |
| 5 | `<`, `>`, `<=`, `>=` |
| 6 | `==`, `!=` |
| 7 | `&`, `^`, `\|` (bitwise) |
| 8 | `&&` |
| 9 | `\|\|` |
| 10 | `??`, `??=` |
| 11 | `?:` |
| 12 | `=`, `+=`, `-=`, etc. |
### Use Parentheses
```csharp
// Clear precedence
if ((a > b) && (c < d))
{
}
// Instead of relying on memory
if (a > b && c < d) // Same, but less clear
```
## Summary
- Arithmetic: `+`, `-`, `*`, `/`, `%`
- Increment/Decrement: `++x`, `x++`, `--x`, `x--`
- Comparison: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Logical: `&&`, `||`, `!`
- Assignment: `=`, `+=`, `-=`, etc.
- Null coalescing: `??`, `??=`, `?.`
- Bitwise: `&`, `|`, `^`, `~`, `<<`, `>>`
- Ternary: `condition ? true : false`
- Use parentheses to clarify precedence
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →