4️⃣ C# Operators – Complete Guide to Expressions, Logic & Overloading
C# operators form the foundation for expressions and computations. From simple math to complex logic and custom class behavior, operators enable developers to write expressive and readable code.
🪂 Introduction – Why Learn C# Operators?
Operators in C# let you manipulate data, compare values, evaluate conditions, and even define custom behavior for your own classes. Whether you’re calculating totals, validating input, or creating powerful expressions, understanding C# operators is key to writing clean and functional code.
🌟 In this guide, you’ll explore:
- Core categories of operators in C#
- How precedence affects expression evaluation
- How to redefine operator behavior for custom types
📃 Topics Covered
Subtopic | Description |
---|---|
➕ C# Operators Overview | Introduction to operator types and usage |
➕ C# Arithmetic Operators | Perform basic math calculations |
➕ C# Assignment Operators | Assign and update variable values |
➕ C# Relational Operators | Compare values for conditional logic |
➕ C# Logical Operators | Combine boolean expressions |
➕ C# Bitwise Operators | Operate at the binary level |
➕ C# Miscellaneous Operators | Specialized operators like ternary, null-coalescing, etc. |
➕ C# Operator Precedence | Understand expression evaluation order |
➕ C# Operator Overloading | Customize operators for user-defined types |
➕ C# Operators Overview
Operators in C# are used to perform operations on variables, constants, and expressions. They include categories such as arithmetic, logical, relational, and assignment operators.
int a = 10, b = 5;
int sum = a + b; // Using arithmetic and assignment operators
➕ C# Arithmetic Operators
These operators are used to perform mathematical calculations:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | a + b | 15 |
- | Subtraction | a - b | 5 |
* | Multiplication | a * b | 50 |
/ | Division | a / b | 2 |
% | Modulus | a % b | 0 |
➕ C# Assignment Operators
These operators assign values and can combine with arithmetic:
Operator | Meaning | Example | Equivalent |
---|---|---|---|
= | Assign | a = 5 | Assign 5 to a |
+= | Add & assign | a += 2 | a = a + 2 |
-= | Subtract & assign | a -= 2 | a = a - 2 |
*= | Multiply & assign | a *= 2 | a = a * 2 |
/= | Divide & assign | a /= 2 | a = a / 2 |
➕ C# Relational Operators
Used to compare values:
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | a == b | false |
!= | Not equal to | a != b | true |
> | Greater than | a > b | true |
< | Less than | a < b | false |
>= | Greater or equal | a >= b | true |
<= | Less or equal | a <= b | false |
➕ C# Logical Operators
Used to combine boolean expressions:
Operator | Description | Example | Result | ||||
---|---|---|---|---|---|---|---|
&& | Logical AND | true && false | false | ||||
` | ` | Logical OR | `true | false` | true | ||
! | Logical NOT | !true | false |
➕ C# Bitwise Operators
Operate at the bit level:
Operator | Description | Example | Result | ||
---|---|---|---|---|---|
& | AND | 5 & 3 | 1 | ||
` | ` | OR | `5 | 3` | 7 |
^ | XOR | 5 ^ 3 | 6 | ||
~ | NOT | ~5 | -6 | ||
<< | Left shift | 5 << 1 | 10 | ||
>> | Right shift | 5 >> 1 | 2 |
➕ C# Miscellaneous Operators
Operator | Purpose | Example |
---|---|---|
?: | Ternary conditional | a > b ? a : b |
?? | Null coalescing | name ?? "Guest" |
?. | Null-conditional access | obj?.ToString() |
typeof | Get type at runtime | typeof(int) |
is | Type checking | obj is string |
as | Safe casting | obj as string |
nameof | Return name of variable as string | nameof(variable) |
➕ C# Operator Precedence
Operators are evaluated in a specific order. For example, multiplication and division have higher precedence than addition and subtraction. Use parentheses to control precedence explicitly.
int result = 10 + 2 * 5; // Result: 20
int adjusted = (10 + 2) * 5; // Result: 60
➕ C# Operator Overloading
C# allows overloading operators in user-defined types to make code more intuitive.
public class Point
{
public int X, Y;
public static Point operator +(Point a, Point b)
{
return new Point { X = a.X + b.X, Y = a.Y + b.Y };
}
}
This enables:
Point p1 = new Point { X = 1, Y = 2 };
Point p2 = new Point { X = 3, Y = 4 };
Point sum = p1 + p2;
📌 Summary – Recap & Next Steps
Understanding operators in C# enables you to write concise, expressive, and optimized code. From basic arithmetic to overloading custom behaviors, operators are the building blocks of logical computation.
🔍 Key Takeaways:
- Operators are categorized as arithmetic, assignment, logical, and more
- Operator precedence affects evaluation order
- You can customize operator behavior for classes via overloading
️️ Real-World Relevance: Operators simplify conditionals, loops, math, and object manipulation in virtually every application.
❓ FAQs
Q: Can all operators be overloaded in C#? ✅ No. You can overload most arithmetic and comparison operators, but not logical operators like &&
, ||
, or =
.
Q: What is the difference between ** and **
in C#? ✅ ==
can be overloaded for custom behavior, while Equals()
checks object equality by default and can be overridden.
Q: Is the null-coalescing operator “ the same as a ternary? ✅ No. ??
only checks for null and returns a default, while ?:
is a general-purpose condition.
Q: How do I avoid ambiguity in complex expressions? ✅ Use parentheses to control operator precedence and clarify evaluation order.
Share Now :