Operators
An operator in C# is a symbol that tells the compiler to perform specific mathematical, logical, or data manipulations on operands (variables or values).
C# provides a rich set of built-in operators. Below are the primary categories of C# Operators
-
Arithmetic Operators
Used to perform common mathematical computations on numeric types
- + (Addition): Adds two values.
- - (Subtraction): Subtracts one value from another.
- * (Multiplication): Multiplies two values.
- / (Division): Divides one value by another.
- % (Modulus): Returns the division remainder.
Static Code Layout Referenceint a = 10;
int b = 3;
Console.WriteLine(a + b); // Output: 13
Console.WriteLine(a - b); // Output: 7
Console.WriteLine(a * b); // Output: 30
Console.WriteLine(a / b); // Output: 3 (Integer division drops decimal part)
Console.WriteLine(a % b); // Output: 1 (Remainder of 10 / 3)
-
Relational (Comparison) Operators
Used to compare two values. These always return a boolean result (true or false)
- == (Equal to): Checks if two values are equal.
- != (Not equal to): Checks if two values are not equal.
- > (Greater than): Checks if the left value is greater than the right.
- < (Less than): Checks if the left value is less than the right.
- >= (Greater than or equal to): Checks left value is greater than or equal to the right.
- <= (Less than or equal to): Checks left value is less than or equal to the right.
Static Code Layout Referenceint x = 5;
int y = 10;
Console.WriteLine(x == y); // Output: False
Console.WriteLine(x != y); // Output: True
Console.WriteLine(x > y); // Output: False
Console.WriteLine(x < y); // Output: True
Console.WriteLine(x >= 5); // Output: True
-
Logical Operators
Used to combine multiple boolean conditions or expressions
- && (Logical AND): Returns true if both statements are true.
- || (Logical OR): Returns true if at least one statement is true.
- ! (Logical NOT): Reverses the logical state (turns true to false and vice versa)
Static Code Layout Referencebool isSunny = true;
bool isWarm = false;
Console.WriteLine(isSunny && isWarm); // Output: False
Console.WriteLine(isSunny || isWarm); // Output: True
Console.WriteLine(!isSunny); // Output: False
-
Assignment Operators
Used to assign values to variables. This includes compound assignments which combine arithmetic operations with an assignment.
- = (Simple Assignment): Assigns right value to left variable.
- += (Add and assign): Shorthand for x = x + y.
- -= (Subtract and assign): Shorthand for x = x - y
- *= (Multiply and assign): Shorthand for x = x * y.
- /= (Divide and assign): Shorthand for x = x / y
Static Code Layout Referenceint score = 10; // Simple assignment
score += 5; // Equivalent to score = score + 5; (score is now 15)
score -= 2; // Equivalent to score = score - 2; (score is now 13)
score *= 2; // Equivalent to score = score * 2; (score is now 26)
Console.WriteLine(score); // Output: 26
-
Increment and Decrement Operators
Unary operators used to increase or decrease a variable's value by exactly
- ++ (Increment): Increases value by 1.
- -- (Decrement): Decreases value by 1
Static Code Layout Referenceint energy = 5;
energy++; // Postfix increment (energy becomes 6)
Console.WriteLine(energy); // Output: 6
--energy;// Prefix decrement (energy becomes 5)
Console.WriteLine(energy); // Output: 5
-
Ternary (Conditional) Operator
A shorthand alternative to the if-else statement. It operates on three operands: condition ? expression_if_true : expression_if_false
Static Code Layout Referenceint age = 20;
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(status); // Output: Adult
-
Null-Coalescing Operators
Operators designed to cleanly handle variables that might hold null values
-
??: Returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand.
-
??=: Assigns the right-hand value to the left-hand variable only if the left-hand variable is currently null
Static Code Layout Referencestring? name = null;
// Use fallback if name is nullstring displayName = name ?? "Guest"; Console.WriteLine(displayName); // Output: Guest
// Assign value only if nullname ??= "Alice"; Console.WriteLine(name);// Output: Alice -