Operator overloading lets your structs and classes use C# operators (+, ==, etc.) naturally.
It's syntactic sugar over static methods — the compiler translates a + b to operator+.
Compound assignment (+=, -=) is auto-derived from the base operator.
Overloading == forces you to also overload != and override Equals + GetHashCode.
Implicit conversions require lossless safety; explicit conversions signal possible data loss.
✦ Definition~90s read
What is Operator Overloading in C#?
Operator overloading lets you define how standard C# operators (+, -, ==, etc.) behave when applied to instances of your own types. Under the hood, the compiler translates a + b into a call to a static op_Addition method, so you're essentially giving custom types the same syntactic convenience as primitives.
★
Imagine you have two LEGO bags.
The core problem it solves is expressiveness: without it, money1 + money2 becomes money1.Add(money2), which obscures intent and breaks the mental model developers have for numeric or value-like types. However, overloading operators without also overriding Equals and GetHashCode is a landmine — HashSet and Dictionary rely on hash codes for O(1) lookups, and if your == operator says two objects are equal but GetHashCode returns different values, you'll get silent duplicates or failed lookups.
This is a common production bug that manifests as 'items not found' or 'duplicate keys allowed' with no exception thrown. C# deliberately blocks overloading certain operators (like &&, ||, [], () ) to prevent ambiguity, but you can enable short-circuit evaluation for && and || by overloading true/false operators alongside & and |.
In practice, operator overloading shines for domain types like Money, Vector, or ComplexNumber, but it's overkill for simple DTOs — use it when the type's identity is its value and arithmetic makes semantic sense. The alternative is explicit method calls (e.g., Add), which are safer but less readable in hot paths like financial calculations or game physics.
Plain-English First
Imagine you have two LEGO bags. You want to 'add' them together to get one big bag of bricks. Normally a computer doesn't know what 'adding two bags' means — it only knows how to add numbers. Operator overloading is you teaching the computer exactly what '+' means for YOUR kind of bag. Once you've done that, writing bag1 + bag2 just works, the same way 3 + 4 works. It's giving a familiar symbol a new job for your custom type.
Most C# developers use operators every day without thinking about it — adding integers, comparing strings, concatenating values. But the moment you build your own types, those same operators go silent. Try adding two Money objects or comparing two Temperature readings and the compiler stares back at you blankly. That friction is exactly the gap operator overloading was designed to close.
The problem isn't just syntax inconvenience. When your custom type can't use natural operators, callers are forced to write verbose method calls like moneyA.Add(moneyB) instead of moneyA + moneyB. That noise accumulates fast, and it breaks the mental model your type is trying to create. A Vector3 that requires vector.CrossProduct(other) instead of vector * other doesn't feel like a vector — it feels like a bag of helper methods. Operator overloading lets your type fulfill its conceptual promise.
By the end of this article you'll understand exactly which operators C# lets you overload and which it forbids, why certain pairs must always be overloaded together, how to implement a real-world Money struct that supports arithmetic and equality cleanly, and the three mistakes that trip up even experienced developers. You'll also walk away ready to answer the operator overloading questions that regularly show up in .NET interviews.
What Operator Overloading Actually Does Under the Hood
When you write 5 + 3 in C#, the compiler translates that into a call to a static method. For built-in types the runtime handles this invisibly, but the mechanism is the same one you use for your own types. Operator overloading is just declaring a special static method with the keyword operator followed by the symbol you're targeting.
The compiler sees moneyA + moneyB, looks for a public static method on the Money type with the signature operator+(Money, Money), and calls it. If it can't find one, you get a compile-time error — not a runtime crash, which is a nice safety net.
This means there's no magic and no performance penalty beyond a normal static method call. The JIT compiler inlines these calls the same way it does any other small static method, so you're not sacrificing speed for readability.
The key constraint is that at least one parameter must be of the type you're defining. You can't hijack the behaviour of int + int from inside your own class. C# specifically protects built-in type semantics from being overridden by user code.
OperatorBasics.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
usingSystem;
// A simple struct representing a temperature readingpublicstructTemperature
{
publicdoubleCelsius { get; }
publicTemperature(double celsius)
{
Celsius = celsius;
}
// The compiler turns 'temp1 + temp2' into this static method call.// Both parameters must relate to our type — here both ARE our type.publicstaticTemperatureoperator +(Temperature left, Temperature right)
{
returnnewTemperature(left.Celsius + right.Celsius);
}
// Scalar multiplication: allow 'temperature * factor'// One parameter is our type, the other is double — that's allowed.publicstaticTemperatureoperator *(Temperature temp, double factor)
{
returnnewTemperature(temp.Celsius * factor);
}
// Override ToString so our output is readablepublicoverridestringToString() => $"{Celsius:F1}°C";
}
classProgram
{
staticvoidMain()
{
var morningTemp = newTemperature(18.5);
var afternoonRise = newTemperature(6.0);
// This calls operator+ behind the scenesTemperature peakTemp = morningTemp + afternoonRise;
Console.WriteLine($"Peak temperature: {peakTemp}");
// This calls operator* — scalar scalingTemperature doubled = morningTemp * 2.0;
Console.WriteLine($"Doubled morning: {doubled}");
}
}
Output
Peak temperature: 24.5°C
Doubled morning: 37.0°C
Under the Hood:
Open the compiled assembly in IL Spy or SharpLab.io and you'll see operator+ compiled to a method named op_Addition. That's the CLS-compliant name the runtime uses. Other languages like F# can call your overloaded operators using these method names directly, which is why naming rules are enforced by the spec.
Production Insight
JIT inlines small operator methods, so there's zero runtime overhead compared to hand-written static methods.
But if your operator method does more than a simple arithmetic operation (e.g., database calls, validation), inlining is suppressed — profiler shows the cost.
Rule: keep operator bodies lean and side-effect-free for the compiler to optimize.
Key Takeaway
Operator overloading is purely syntactic sugar for a static method.
The compiler enforces type safety — no surprise overloads from other types.
There is zero runtime penalty when the operator is small and JIT-inlinable.
thecodeforge.io
Operator Overloading and GetHashCode in C#
Operator Overloading Csharp
Building a Real-World Money Type — Arithmetic and Equality Done Right
The most convincing demo of operator overloading isn't a math vector — it's a Money type. Money has rules that match how operators feel: you can add two USD amounts, but adding USD to EUR should throw. That real-world constraint shows you how to put business logic inside an operator, not just delegate to a constructor.
Equality is where most developers stumble. C# has two separate equality concepts: reference equality (are these the same object in memory?) and value equality (do these represent the same value?). For a struct, the default == checks value equality field-by-field using reflection, which is both slow and fragile. For a class, the default == is reference equality, which is almost never what you want for a value-oriented type like Money.
The rule is non-negotiable: if you overload ==, you MUST also overload !=. The compiler enforces this. And whenever you overload ==, you should also override Equals(object) and GetHashCode(), because LINQ, dictionaries, and HashSets all rely on those methods, not on your operator.
This section shows a complete Money struct that gets all of this right, so you have a real template to copy from.
Money.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
usingSystem;
usingSystem.Collections.Generic;
publicreadonlystructMoney : IEquatable<Money>
{
publicdecimalAmount { get; }
publicstringCurrency { get; }
publicMoney(decimal amount, string currency)
{
if (string.IsNullOrWhiteSpace(currency))
thrownewArgumentException("Currency code cannot be empty.", nameof(currency));
Amount = Math.Round(amount, 2); // Money is always 2 decimal placesCurrency = currency.ToUpperInvariant();
}
// ── Arithmetic operators ────────────────────────────────────────────publicstaticMoneyoperator +(Money left, Money right)
{
// Business rule lives HERE, not scattered across callersEnsureSameCurrency(left, right, "+");
returnnewMoney(left.Amount + right.Amount, left.Currency);
}
publicstaticMoneyoperator -(Money left, Money right)
{
EnsureSameCurrency(left, right, "-");
returnnewMoney(left.Amount - right.Amount, left.Currency);
}
// Scale by a factor — e.g. applying a tax ratepublicstaticMoneyoperator *(Money money, decimal factor)
{
returnnewMoney(money.Amount * factor, money.Currency);
}
// Allow factor * money as well (commutativity for the caller's convenience)publicstaticMoneyoperator *(decimal factor, Money money) => money * factor;
publicstaticbooloperator >(Money left, Money right)
{
EnsureSameCurrency(left, right, ">");
return left.Amount > right.Amount;
}
publicstaticbooloperator <(Money left, Money right)
{
EnsureSameCurrency(left, right, "<");
return left.Amount < right.Amount;
}
// ── Equality operators ──────────────────────────────────────────────// Rule: overload == → must also overload !=// Rule: overload == → must also override Equals and GetHashCodepublicstaticbooloperator ==(Money left, Money right) => left.Equals(right);
publicstaticbooloperator !=(Money left, Money right) => !left.Equals(right);
// IEquatable<Money> — used by List.Contains, LINQ, etc. without boxingpublicboolEquals(Money other)
=> Amount == other.Amount &&
string.Equals(Currency, other.Currency, StringComparison.OrdinalIgnoreCase);
// Required override when Equals is overridden — used by dictionaries/hashsetspublicoverrideboolEquals(object? obj)
=> obj isMoney other && Equals(other);
publicoverrideintGetHashCode()
=> HashCode.Combine(Amount, Currency.ToUpperInvariant());
publicoverridestringToString() => $"{Currency} {Amount:F2}";
// ── Private helpers ─────────────────────────────────────────────────privatestaticvoidEnsureSameCurrency(Money left, Money right, string op)
{
if (!string.Equals(left.Currency, right.Currency, StringComparison.OrdinalIgnoreCase))
thrownewInvalidOperationException(
$"Cannot apply '{op}' to {left.Currency} and {right.Currency}. " +
$"Convert to the same currency first.");
}
}
classProgram
{
staticvoidMain()
{
var price = newMoney(19.99m, "USD");
var shipping = newMoney(4.99m, "USD");
var taxRate = 0.08m; // 8% taxMoney subtotal = price + shipping; // calls operator+Money tax = subtotal * taxRate; // calls operator*Money total = subtotal + tax;
Console.WriteLine($"Price: {price}");
Console.WriteLine($"Shipping: {shipping}");
Console.WriteLine($"Tax (8%): {tax}");
Console.WriteLine($"Total: {total}");
Console.WriteLine();
// Equality check works correctlyvar duplicatePrice = newMoney(19.99m, "USD");
Console.WriteLine($"price == duplicatePrice: {price == duplicatePrice}");
Console.WriteLine($"price == total: {price == total}");
Console.WriteLine();
// Works correctly in a HashSet because GetHashCode is consistent with Equalsvar priceSet = newHashSet<Money> { price, shipping, duplicatePrice };
Console.WriteLine($"HashSet count (price added twice): {priceSet.Count}");
Console.WriteLine();
// Comparison operatorsConsole.WriteLine($"total > price: {total > price}");
// This would throw — different currenciestry
{
var euros = newMoney(10.00m, "EUR");
var _ = price + euros;
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"Expected error: {ex.Message}");
}
}
}
Output
Price: USD 19.99
Shipping: USD 4.99
Tax (8%): USD 2.00
Total: USD 26.98
price == duplicatePrice: True
price == total: False
HashSet count (price added twice): 2
total > price: True
Expected error: Cannot apply '+' to USD and EUR. Convert to the same currency first.
Watch Out:
If you override Equals but forget GetHashCode, your type will silently break inside Dictionary<K,V> and HashSet<T>. Two Money values that are Equal could hash to different buckets, causing lookups to fail even when the key is logically present. The compiler will warn you (CS0659), but the bug is real and confusing — always override both together.
Production Insight
In a real payments system, missing GetHashCode caused a reconciliation job to incorrectly flag 2% of transactions as unmatched overnight.
The bug was hard to spot because operator== alone worked fine in unit tests — the problem only surfaced in HashSet and Dictionary usage.
Rule: always treat compiler warning CS0659 as an error and write a unit test that exercises HashSet with a duplicate.
Key Takeaway
Overload == and != together.
Override Equals and GetHashCode together.
Implement IEquatable<T> to avoid boxing on structs.
Test your type in HashSet and Dictionary before shipping.
Which Operators You Can Overload — and the Ones C# Deliberately Blocks
C# gives you a generous but not unlimited set of operators to overload. The choices reflect a deliberate philosophy: operators that control program flow, assignment, or type conversion are off-limits to prevent code that's impossible to reason about.
The operators you CAN overload fall into three groups. Unary operators: +, -, !, ~, ++, --. Binary arithmetic: +, -, *, /, %, &, |, ^, <<, >>. Comparison: ==, !=, <, >, <=, >= (these must be overloaded in symmetric pairs — you can't overload < without also overloading >).
The operators C# deliberately BLOCKS include: &&, ||, =, +=, -=, new, typeof, is, as, and the ternary ?:. The compound assignment operators (+=, -=, etc.) are intentionally excluded because C# derives them automatically. Once you define operator+, the compiler generates += for free. You never write it yourself.
One special case worth knowing: the true and false operators. These are rarely used, but overloading them enables your type to participate in short-circuit && and || evaluation. It's an advanced pattern used in libraries that model nullable or tri-state logic.
OperatorRulesDemo.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
usingSystem;
publicstructPercentage
{
public double Value { get; } // 0.0 to 100.0publicPercentage(double value)
{
// Clamp silently — percentages can't exceed 100% or go below 0%Value = Math.Clamp(value, 0.0, 100.0);
}
// Unary negation — "remaining percentage"// e.g. !Percentage(30) → Percentage(70) means "70% remaining"publicstaticPercentageoperator !(Percentage p)
=> newPercentage(100.0 - p.Value);
// Unary increment — nudge up by 1 pointpublicstaticPercentageoperator ++(Percentage p)
=> newPercentage(p.Value + 1.0);
// Binary additionpublicstaticPercentageoperator +(Percentage left, Percentage right)
=> new Percentage(left.Value + right.Value); // clamped by constructor// Comparison pair — C# REQUIRES both < and > if you want eitherpublicstaticbooloperator <(Percentage left, Percentage right)
=> left.Value < right.Value;
publicstaticbooloperator >(Percentage left, Percentage right)
=> left.Value > right.Value;
// Equality pair — C# REQUIRES both == and != if you want eitherpublicstaticbooloperator ==(Percentage left, Percentage right)
=> Math.Abs(left.Value - right.Value) < 0.0001; // float-safe comparisonpublicstaticbooloperator !=(Percentage left, Percentage right)
=> !(left == right);
publicoverrideboolEquals(object? obj)
=> obj isPercentage other && this == other;
publicoverrideintGetHashCode() => Math.Round(Value, 4).GetHashCode();
publicoverridestringToString() => $"{Value:F1}%";
}
classProgram
{
staticvoidMain()
{
var discount = newPercentage(30.0);
var extraOff = newPercentage(15.0);
// += is automatically derived from operator+ — we never wrote itPercentage combined = discount;
combined += extraOff;
Console.WriteLine($"Combined discount: {combined}");
// Unary ! gives us the "remaining" percentagePercentage customerPays = !combined;
Console.WriteLine($"Customer pays: {customerPays}");
// ++ operatorPercentage bumped = discount;
bumped++;
Console.WriteLine($"Bumped discount: {bumped}");
// Clamping — adding beyond 100% is safevar huge = newPercentage(80.0) + newPercentage(80.0);
Console.WriteLine($"Clamped result: {huge}");
// ComparisonConsole.WriteLine($"discount > extraOff: {discount > extraOff}");
// Demonstrating that == and != must be pairedConsole.WriteLine($"discount == extraOff: {discount == extraOff}");
Console.WriteLine($"discount != extraOff: {discount != extraOff}");
}
}
Output
Combined discount: 45.0%
Customer pays: 55.0%
Bumped discount: 31.0%
Clamped result: 100.0%
discount > extraOff: True
discount == extraOff: False
discount != extraOff: True
Pro Tip:
You never need to define += explicitly. The moment you define operator+, C# synthesises compound assignment for free. If you try to define operator+= yourself you'll get a compile error. This is by design — it prevents a class where a + b and a += b could behave differently, which would be a maintenance nightmare.
Production Insight
Compound assignment auto-derivation means you cannot have a += b behave differently from a + b.
This is good: it prevents a common source of bugs where the two forms diverge.
But it also means you cannot optimize += for mutable types — if your class is mutable and you want in-place mutation, you must not use operator overloading; use a dedicated method like AddInPlace.
Key Takeaway
C# blocks assignment, conditional, and flow-control operators for safety.
Compound assignment operators are auto-derived from base operators — you never write them.
Comparison operators must be overloaded in symmetric pairs.
Conversion Operators — Letting Your Type Speak Other Type Languages
Operator overloading has a close sibling that often gets forgotten: conversion operators. These let you define what happens when code tries to cast or implicitly assign your type to another type. There are two flavours: implicit (no cast syntax needed, compiler inserts it silently) and explicit (caller must write the cast, signalling they understand precision might be lost).
The rule of thumb is pragmatic: use implicit conversion only when it is completely safe and lossless — the kind of conversion where no reasonable caller would ever want to be warned. Use explicit when data can be truncated, precision is lost, or an exception might be thrown.
A great real-world example is a Celsius type that converts to Fahrenheit. That conversion is always possible but the result is a different scale, so explicit makes sense. Going the other way, converting from a raw double to Celsius, could be implicit because it's just wrapping a value with no loss.
Conversion operators compose naturally with your arithmetic operators. Once you have a rich conversion story, your type starts to feel like a first-class citizen of the language, not an awkward guest.
ConversionOperators.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
usingSystem;
publicreadonlystructCelsius
{
publicdoubleDegrees { get; }
publicCelsius(double degrees) => Degrees = degrees;
// IMPLICIT: wrapping a double in Celsius is always safe — no information lost// Caller can write: Celsius temp = 100.0; (no cast needed)publicstaticimplicitoperatorCelsius(double degrees)
=> newCelsius(degrees);
// EXPLICIT: converting to Fahrenheit changes the scale.// Caller must write: double f = (double)temp; to acknowledge the conversion.// We return double because Fahrenheit is just a double to the outside world here.publicstaticexplicitoperatordouble(Celsius celsius)
=> (celsius.Degrees * 9.0 / 5.0) + 32.0;
// Arithmetic still works — combining with previous section's lessonspublicstaticCelsiusoperator +(Celsius left, Celsius right)
=> newCelsius(left.Degrees + right.Degrees);
publicoverridestringToString() => $"{Degrees:F2}°C";
}
// A lightweight struct that ONLY holds Fahrenheit — shows cross-type conversionpublicreadonlystructFahrenheit
{
publicdoubleDegrees { get; }
publicFahrenheit(double degrees) => Degrees = degrees;
// Explicit conversion FROM Celsius TO FahrenheitpublicstaticexplicitoperatorFahrenheit(Celsius c)
=> newFahrenheit((c.Degrees * 9.0 / 5.0) + 32.0);
// Explicit conversion back from Fahrenheit to CelsiuspublicstaticexplicitoperatorCelsius(Fahrenheit f)
=> newCelsius((f.Degrees - 32.0) * 5.0 / 9.0);
publicoverridestringToString() => $"{Degrees:F2}°F";
}
classProgram
{
staticvoidMain()
{
// Implicit conversion — no cast syntax requiredCelsius boiling = 100.0; // implicit operator Celsius(double) fires hereConsole.WriteLine($"Boiling point: {boiling}");
// Explicit conversion to double — caller must acknowledge scale changedouble boilingF = (double)boiling;
Console.WriteLine($"In Fahrenheit (as double): {boilingF:F2}");
// Explicit conversion to Fahrenheit structFahrenheit boilingFahrenheit = (Fahrenheit)boiling;
Console.WriteLine($"In Fahrenheit (as struct): {boilingFahrenheit}");
// Round-trip: Fahrenheit → Celsius → back to Fahrenheitvar bodyTemp = newFahrenheit(98.6);
Celsius bodyTempC = (Celsius)bodyTemp;
Fahrenheit roundTrip = (Fahrenheit)bodyTempC;
Console.WriteLine($"Body temp round-trip: {bodyTemp} → {bodyTempC} → {roundTrip}");
// Arithmetic on Celsius still works naturallyCelsius morning = 15.0; // implicitCelsius afternoon = 8.0; // implicitCelsius peak = morning + afternoon;
Console.WriteLine($"Peak: {peak}");
}
}
Output
Boiling point: 100.00°C
In Fahrenheit (as double): 212.00
In Fahrenheit (as struct): 212.00°F
Body temp round-trip: 98.60°F → 37.00°C → 98.60°F
Peak: 23.00°C
Watch Out:
Implicit conversions can cause hard-to-spot bugs if you're too generous with them. If you make a Celsius-to-Fahrenheit conversion implicit, a method expecting Celsius silently accepts a Fahrenheit value, does the math in the wrong scale, and produces a wrong answer with no compiler warning. Reserve implicit only for conversions where the types are genuinely interchangeable from the caller's perspective.
Production Insight
A team once defined implicit conversion from string to a CustomerId type. A method overload expecting CustomerId started receiving raw strings from a legacy caller — the strings were treated as valid IDs, causing data corruption because the strings were not actually valid customer references.
The silent conversion made the bug nearly impossible to trace. They changed to explicit conversion and added unit tests.
Rule: implicit conversions should only exist when you'd be comfortable removing the type entirely and replacing it with the source type in all public APIs.
Key Takeaway
Implicit conversion = lossless, safe, and caller never needs to think about it.
Explicit conversion = caller must acknowledge the conversion with a cast.
Overusing implicit conversions leads to silent bugs — always prefer explicit unless the conversion is truly identity-preserving.
The true and false Operators — Enabling Short-Circuit Evaluation in Custom Types
Most developers never need the true and false operators. They are the gateway to making your type work with && and || in short-circuit evaluation. Without them, you cannot use your custom type in logical expressions the way nullable Booleans do.
The use case is modeling a tri-state Boolean — a value that can be true, false, or indeterminate. Libraries like nullable Booleans in databases, or condition models in rule engines, use this pattern. The compiler requires both true and false operators to be defined together. Once you have them, you can use the & and | operators combined with them, and then && and || become available.
Here's how it works: for &&, the compiler evaluates the left operand using the true operator. If it returns true, it evaluates the right operand; otherwise it short-circuits to false. For ||, it uses the false operator similarly. This is how nullable bools like bool? work internally.
Let's implement a simple ThreeState type that supports logical operations.
ThreeStateBoolean.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
usingSystem;
// Models a three-state logical value: True, False, IndeterminatepublicstructThreeState
{
// Internal representation:// 1 = True, -1 = False, 0 = Indeterminateprivatereadonlysbyte _value;
privateThreeState(sbyte value) => _value = value;
publicstaticreadonlyThreeStateTrue = newThreeState(1);
publicstaticreadonlyThreeStateFalse = newThreeState(-1);
publicstaticreadonlyThreeStateIndeterminate = newThreeState(0);
// The 'true' operator: returns true if the value is definitely truepublicstaticbooloperatortrue(ThreeState x) => x._value == 1;
// The 'false' operator: returns true if the value is definitely falsepublicstaticbooloperatorfalse(ThreeState x) => x._value == -1;
// Logical AND: & operator (needed before && can be used)publicstaticThreeStateoperator &(ThreeState left, ThreeState right)
{
// If either is false, result is falseif (left._value == -1 || right._value == -1)
returnFalse;
// If either is indeterminate, result is indeterminateif (left._value == 0 || right._value == 0)
returnIndeterminate;
// Both are truereturnTrue;
}
// Logical OR: | operator (needed before || can be used)publicstaticThreeStateoperator |(ThreeState left, ThreeState right)
{
// If either is true, result is trueif (left._value == 1 || right._value == 1)
returnTrue;
// If either is indeterminate, result is indeterminateif (left._value == 0 || right._value == 0)
returnIndeterminate;
// Both are falsereturnFalse;
}
// Logical NOT: ! operatorpublicstaticThreeStateoperator !(ThreeState x)
{
return x._value switch
{
1 => False,
-1 => True,
_ => Indeterminate
};
}
// Override Equals/GetHashCode for consistencypublicoverrideboolEquals(object? obj) => obj isThreeState other && _value == other._value;
publicoverrideintGetHashCode() => _value.GetHashCode();
publicoverridestringToString() => _value switch
{
1 => "True",
-1 => "False",
_ => "Indeterminate"
};
}
classProgram
{
staticvoidMain()
{
var t = ThreeState.True;
var f = ThreeState.False;
var i = ThreeState.Indeterminate;
// Short-circuit && works because true/false operators are defined// The compiler generates: if (t) then evaluate f else falseConsole.WriteLine($"t && f: {t && f}"); // FalseConsole.WriteLine($"f && t: {f && t}"); // False (short-circuits, no evaluation of right)Console.WriteLine($"t || f: {t || f}"); // True (short-circuits, no evaluation of right)Console.WriteLine($"f || t: {f || t}"); // TrueConsole.WriteLine($"i && t: {i && t}"); // ? (Indeterminate, because i is not true nor false)// For i && t: false operator on i returns false (i is not definitely false), so conditional evaluates right side.// i && t resolves to i & t = Indeterminate & True = IndeterminateConsole.WriteLine($"!t: {!t}");
}
}
Output
t && f: False
f && t: False
t || f: True
f || t: True
i && t: Indeterminate
!t: False
Mental Model: Short-Circuit Operators and true/false
For a type to support &&, it must define & and both true/false operators.
For ||, define | and the same true/false operators.
The compiler uses the true operator to decide whether to short-circuit in && (if left is not true, skip right).
Use this pattern for nullable, tri-state, or fuzzy logic types.
Production Insight
A rule engine defined a custom ConditionResult type with true/false operators for short-circuit evaluation of complex business rules.
During a deployment, a bug in the true operator caused && to always evaluate both sides, effectively disabling short-circuit logic for 30% of rules.
The latency of rule evaluation increased by 200ms per transaction as a result.
Fix: unit test the short-circuit behavior explicitly by passing a condition that has a side effect (like counting evaluations) and verify only the necessary side effects occur.
Key Takeaway
true and false operators enable your type to participate in short-circuit && and ||.
Define & and | along with true/false as a pair.
Without them, you can only use & and | (non-short-circuit).
This is an advanced pattern — only use it when tri-state or nullable logic is required.
Overloading Binary Operators — Why Your Code Smells Without Them
Binary operators are the workhorses of arithmetic. If you're writing a Money type and you've got Add(Money other) instead of +, you're writing java-with-training-wheels. C# gives you +, -, *, /, %. Use them.
The compiler rewrites a + b into a static method call. That public static Money operator +(Money left, Money right) isn't magic — it's a named function the compiler knows to call when it sees the plus sign. The operands are the parameters, the return type is the result. No exceptions for null unless you write them.
Here's the trap I've seen catch five juniors: binary operators must return the same type as at least one operand. You can't write operator +(Order, Product) that returns a decimal. That's not operator overloading, that's a static method wearing a costume. Keep the contract tight: additive operators return something that can be chained into another expression.
Never overload + to mean concatenation for a numeric type. I've seen a Money + Money implementation that summed amounts but appended currency strings. The accounting system accepted it for three months. The auditors were not amused.
Key Takeaway
Binary operators must return the same type as one operand and must not mutate the operands. Immutable structs keep this contract honest.
Overloading Unary Operators — The One-Liner That Changes Flow
Unary operators (++, --, !, ~, +, -) take one operand. They look trivial but they're the difference between counter = counter.Increment() and counter++. The latter is cleaner, but only if you respect the semantics.
For ++ and --, the compiler generates separate code for prefix vs postfix. The prefix form returns the new value; postfix returns the old. C# handles this by calling your overload and then either returning the result (prefix) or taking a snapshot first (postfix). You just write one method — the compiler does the bookkeeping.
But here's where production code bites back: ++ on a mutable struct. Don't. Your overload creates a new instance. The caller expects the original to change. Use readonly struct or a class. Pick one. Every time I see a struct with a mutable ++ overload, I find a race condition within fifty lines.
! and ~ are great for flag enums or validation results. Overload ! to mean "is invalid" on a ValidationResult type. It reads like natural language: if (!validationResult).
UnaryOperatorsInProduction.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// io.thecodeforge — csharp tutorialpublicreadonlystructCounter
{
publicintValue { get; }
publicCounter(int value) => Value = value;
// One method handles both prefix (++x) and postfix (x++)publicstaticCounteroperator ++(Counter c) => newCounter(c.Value + 1);
// Overload ! to mean "is zero" — domain-specificpublicstaticbooloperator !(Counter c) => c.Value == 0;
}
// Usage:var c = newCounter(5);
c++; // postfix: returns old value (5), c becomes 6
++c; // prefix: returns new value (7), c becomes 7Console.WriteLine(c.Value); // 7Console.WriteLine(!c); // False (Value is not 0)var zero = newCounter(0);
Console.WriteLine(!zero); // True
Output
7
False
True
Senior Shortcut:
If you overload ++, also overload == and !=. Otherwise counter++ == 5 is a compile error or confusing behavior. These operators travel in packs.
Key Takeaway
Unary operator overloads must return the same type as the operand. For ++ on a struct, the struct must be immutable — the return is a new instance, not a mutation.
Conversion Operators — Implicit vs Explicit: Don't Let the Compiler Guess Wrong
You've written a custom Money type. Now your boss wants to pass it to a legacy API that takes decimal. You could expose a .ToDecimal() method, sure. But that's noise. The real solution: conversion operators.
Conversion operators tell the compiler how to treat your type as another type — either implicitly (no cast required) or explicitly (requires (TargetType) cast). The rule of thumb: if the conversion can lose data or throw, make it explicit. If it's always safe, implicit is fine.
Implicit conversions look clean but hide bugs. Explicit conversions look ugly but signal risk. Your Money to decimal is safe — always exact. Implicit. But decimal to Money? Currency rounding might truncate. That's explicit territory. C# forces you to pick. Pick wisely — your code reviewers will thank you.
ConversionExample.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// io.thecodeforge — csharp tutorialpublicreadonlystructMoney
{
privatereadonlydecimal _amount;
privatereadonlystring _currency;
publicMoney(decimal amount, string currency)
{
_amount = amount;
_currency = currency;
}
// Safe — no data losspublicstaticimplicitoperatordecimal(Money m) => m._amount;
// Risky — we drop currency and may roundpublicstaticexplicitoperatorMoney(decimal d) =>
newMoney(Math.Round(d, 2), "USD");
}
var price = newMoney(19.99m, "USD");
decimal value = price; // implicit — reads clearly
var fromDecimal = (Money)20.001m; // explicit — you asked for it// fromDecimal._amount == 20.00
Output
// No output — compile-time behavior. Implicit conversion compiles.
Don't make a conversion implicit if it throws. That violates the Principle of Least Surprise. Your junior dev will wonder why a simple assignment crashes at 3 AM.
Key Takeaway
Implicit for safe, lossless conversions. Explicit for anything that can fail or lose data. Never implicit + throw.
The true and false Operators — Enabling Short-Circuit Evaluation in Custom Types
C# lets you overload && and || for your own types. But there's a catch: the language forces you to implement the true and false operators first. Why? Because short-circuit evaluation needs a binary decision — is this value truthy or falsy?
Think of it like nullable bools. Nullable<bool> has three states: true, false, null. When you write nullableBool && somethingElse, the compiler can't short-circuit unless it knows for sure the first operand is false. That's exactly what the true and false operators provide: a deterministic yes/no answer.
Your custom type — say a Validated<T> — can overload these to support clean conditional logic. When you write if (validated) { ... }, the compiler calls operator true. When you write if (!validated) { ... }, it calls operator false. Implement them correctly, and your type becomes a first-class citizen in boolean expressions.
TrueFalseExample.csCSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// io.thecodeforge — csharp tutorialpublicreadonlystructValidated<T>
{
privatereadonly T _value;
privatereadonlybool _isValid;
publicValidated(T value, bool isValid)
{
_value = value;
_isValid = isValid;
}
publicstaticbooloperatortrue(Validated<T> v) => v._isValid;
publicstaticbooloperatorfalse(Validated<T> v) => !v._isValid;
// Required for && to workpublicstaticValidated<T> operator &(Validated<T> a, Validated<T> b) =>
newValidated<T>(a._isValid ? b._value : a._value, a._isValid && b._isValid);
}
var a = newValidated<int>(42, true);
var b = newValidated<int>(0, false);
if (a && b) Console.WriteLine("Both valid");
elseConsole.WriteLine("Short-circuited");
// Output: Short-circuited — never evaluates b since a is false? No, a is true.// b operator false is called, & operator short-circuits via false.
Output
Short-circuited
Senior Shortcut:
Implement operator false first — it's simpler. The compiler uses it to decide if the right side of && can be skipped. If operator false returns true, short-circuit happens. Done.
Key Takeaway
Overload true/false to make your type work with && and ||. Every custom boolean logic system in C# goes through this gate.
● Production incidentPOST-MORTEMseverity: high
The Missing GetHashCode That Broke Payment Reconciliation
Symptom
Payment reconciliation ran nightly: it collected all transaction amounts into a HashSet<Money>, expected deduplication, but ended up with duplicates. The HashSet.Count was always higher than expected, causing mismatches that took hours to track down.
Assumption
The team assumed that overloading == and implementing IEquatable<Money> would be sufficient for HashSet deduplication. They read that HashSet uses GetHashCode internally but didn't realize the compiler enforces consistency — and the warning CS0659 was dismissed as a false positive.
Root cause
Money struct had operator== and Equals overridden, but GetHashCode was not overridden. Two Money objects with same amount and currency returned different hash codes (the default from struct’s value-based hash was unreliable across runs). HashSet placed equal objects into different buckets, so Contains() returned false and duplicates were allowed.
Fix
Override GetHashCode using HashCode.Combine(Amount, Currency) and add IEquatable<Money>. Rebuild the HashSet — the Count dropped to expected values and reconciliation matched.
Key lesson
Overriding == without GetHashCode is a data-corruption bug, not a warning.
Treat compiler warning CS0659 as a hard error.
Always implement IEquatable<T> on structs to avoid boxing.
Test your type in a HashSet before production.
Production debug guideSymptom → Action pairs for the most common operator overloading bugs5 entries
Symptom · 01
HashSet contains duplicate elements, Dictionary lookup fails even though key exists
→
Fix
Check if type overrides GetHashCode. Open the type definition: if GetHashCode is missing or uses default struct implementation, override it using HashCode.Combine.
Symptom · 02
Compile error CS0216: 'type' requires matching operator '!=' to also be defined
→
Fix
C# enforces that == and != must be overloaded as a pair. Add the missing operator. Usually delegate to !left.Equals(right).
Symptom · 03
Implicit conversion results in wrong method overload being called
→
Fix
Too many implicit conversions cause ambiguity errors or silent wrong calls. Change implicit to explicit using (ExplicitType)value at call sites, or add a diagnostic overload.
Symptom · 04
operator+ works but += fails: 'the left-hand side of an assignment must be a variable'
→
Fix
If the type is a class, += tries to mutate the original reference. Ensure the type is a readonly struct so compound assignment returns a new instance that can be assigned.
Symptom · 05
Comparing two equal Money structs returns false when using == in a generic method
→
Fix
Generic methods use object.Equals at runtime, not operator==. Override Equals and GetHashCode, and constrain generic types with IEquatable<T>.
★ Operator Overloading Quick Debug Cheat SheetFive operator-related failures and the exact fix
HashSet allows duplicates after overloading ==−
Immediate action
Run a quick test: create two equal objects and check if hashSet.Contains(second) returns false.
Isolate the line where the conversion happens, inspect parameter types.
Commands
Change implicit to explicit in the type definition, then fix all call sites.
Use Roslyn analyser: `dotnet build /warnaserror:CS1061,CS1501` to surface hidden overload resolution.
Fix now
Make conversion explicit unless it's truly lossless.
+= does not compile for a struct+
Immediate action
Check if the struct is marked readonly. If not, make it readonly.
Commands
Add `readonly` modifier to the struct declaration.
Remove any mutable fields; use readonly properties.
Fix now
Change to readonly struct – += will then work by assigning a new copy.
Dynamic dispatch bypasses overloaded operator+
Immediate action
Check if a variable is typed as `dynamic` or `object`.
Commands
Replace `dynamic` with the concrete type, or use `switch` pattern matching.
If using object, cast to the concrete type before using operator.
Fix now
Avoid dynamic/object for types with overloaded operators.
Operator Overloading vs Regular Methods
Aspect
Operator Overloading
Regular Methods
Syntax at call site
price + tax (natural, concise)
price.Add(tax) (verbose, method-flavoured)
Discoverability
Relies on docs or IDE hints — not obvious a '+' is defined
Shows up in IntelliSense autocomplete immediately
Best fit for
Value types that model a real-world quantity (Money, Vector, Temperature)
Operations with side effects, multiple return values, or complex parameters
Compound assignment (+=)
Automatically derived once operator+ exists — you write nothing extra
Must write AddInPlace or similar method manually
Interoperability
Other CLR languages call op_Addition by name — works but ugly
Universally callable from any CLR language using standard syntax
Enforcement of symmetry
C# forces you to define == and != together, < and > together
No automatic pairing — easy to forget the inverse method
Risk of misuse
Can create confusing code if semantics don't match the symbol
Method name is always explicit — harder to mislead
Performance
Inlined by JIT — effectively zero overhead for simple struct operations
Same — also inlined for simple cases, no meaningful difference
Key takeaways
1
Operator overloading compiles to a static method named op_Addition, op_Equality, etc.
it's ordinary method dispatch with no hidden magic or runtime cost.
2
Overloading == forces you to also overload != (compiler error otherwise), and you must override Equals + GetHashCode to stay consistent with LINQ, Dictionary, and HashSet behaviour.
3
Compound assignment operators (+=, -=, *=) are automatically synthesised by the compiler once you define the base operator
you never write them yourself, and attempting to do so is a compile error.
4
Use implicit conversions only when the conversion is completely lossless and safe; use explicit when precision is lost, the scale changes, or an exception is possible
this keeps callers informed of meaningful type boundaries.
5
Overload operators on readonly structs for correct immutable semantics; avoid mutable classes unless you document the reference replacement behavior explicitly.
Common mistakes to avoid
5 patterns
×
Overloading == without overriding Equals and GetHashCode
Symptom
Your == works in simple comparisons, but LINQ's .Contains(), Dictionary lookups, and HashSet deduplication use Equals/GetHashCode internally, not your operator. The symptom is a Dictionary not finding a key that you know is there, or a HashSet that contains duplicates.
Fix
Always override Equals(object) and GetHashCode() whenever you overload ==, and implement IEquatable<T> so LINQ uses the typed, non-boxing path.
×
Making implicit conversion operators too broad
Symptom
You define an implicit conversion from string to your type to avoid cast syntax, and suddenly every method that accepts your type silently accepts raw strings. The symptom is a wrong overload resolving at runtime, or a null string being converted and throwing inside your constructor with a confusing stack trace.
Fix
Use implicit only when the source and target types are conceptually the same thing. If there's any doubt, make it explicit.
×
Overloading operators on mutable classes instead of structs
Symptom
You overload + on a class, and the operator creates a new instance as expected. But a junior developer on your team writes account += deposit thinking it modifies account in place — it actually replaces the reference with a new object, silently discarding any other references to the original object.
Fix
Model value types (Money, Vector, Percentage) as readonly struct. The immutability makes operator semantics correct by design — operators always return new values, never mutate.
×
Forgetting to implement IEquatable<T> on structs
Symptom
Using List.Contains() or Array.IndexOf() on a struct with overloaded == causes boxing and slower performance. The equality check still uses value semantics via the virtual Equals method, but every time the struct is boxed to object, memory allocation occurs.
Fix
Implement IEquatable<T> on your struct. The generic interface allows typed calls without boxing.
×
Defining operator+ but not operator- (or vice versa) on a numeric-like type
Symptom
Callers expect both addition and subtraction to be available for a numeric type. If only one is defined, the type feels incomplete and forces callers to use makeshift workarounds.
Fix
For types that represent numbers or quantities, define all four arithmetic operators (+, -, *, /) if they make sense.
INTERVIEW PREP · PRACTICE MODE
Interview Questions on This Topic
Q01SENIOR
Why does C# require you to overload == and != together, and what happens...
Q02SENIOR
What is the difference between implicit and explicit conversion operator...
Q03SENIOR
Can you overload the += operator in C#? How does compound assignment act...
Q04SENIOR
Explain how the true and false operators enable short-circuit && and || ...
Q05SENIOR
Why is it recommended to overload operators on structs rather than class...
Q01 of 05SENIOR
Why does C# require you to overload == and != together, and what happens if you override Equals without also overriding GetHashCode?
ANSWER
C# requires overloading == and != together to ensure logical consistency: if two objects are equal, they cannot be unequal. The compiler enforces this pairing. If you override Equals without overriding GetHashCode, two equal objects may hash to different buckets in a hash table, causing lookups to fail even though the key is logically present. You get a compiler warning (CS0659) because the default GetHashCode from Object uses reference equality for classes or field-by-field value hashing for structs, which can differ from your custom equality. Always override GetHashCode using the same fields as Equals, typically via HashCode.Combine.
Q02 of 05SENIOR
What is the difference between implicit and explicit conversion operators in C#? Give a concrete example of when you'd choose each.
ANSWER
Implicit conversion operators allow the compiler to convert from one type to another without any explicit cast, while explicit conversion operators require the developer to write a cast. The key decision is whether the conversion is always safe and lossless. Example: converting a double to a Celsius struct — wrapping a double in a struct adds no semantic change, so implicit is fine. Converting Celsius to Fahrenheit changes the scale and introduces a different unit — explicit forces the caller to acknowledge the conversion: double f = (double)celsiusTemp;. If you made that implicit, a method expecting Fahrenheit could receive Celsius silently and produce wrong calculations. Reserve implicit for truly identity-preserving conversions.
Q03 of 05SENIOR
Can you overload the += operator in C#? How does compound assignment actually work for overloaded types?
ANSWER
You cannot directly overload += in C# — attempting to do so causes a compile error. The C# compiler automatically synthesises compound assignment operators from the corresponding binary operator. For example, if you define operator+, the compiler generates operator+= that calls operator+ and assigns the result back. This ensures that a + b and a += b are always consistent. If you need different behavior for in-place mutation, do not use operator overloading; use a dedicated method like AddInPlace.
Q04 of 05SENIOR
Explain how the true and false operators enable short-circuit && and || in C#.
ANSWER
The true and false operators are used by the compiler to evaluate short-circuit logical operations. For x && y, the compiler first calls the true operator on x. If it returns true, it evaluates y and applies the & operator. If it returns false (i.e., x is not definitely true), the entire && expression short-circuits to false. For x || y, the compiler uses the false operator: if x is not definitely false, it short-circuits to true; otherwise it evaluates y and applies |. To make this work, you must define both the true and false operators, plus the & and | operators. This pattern is used by nullable bools and tri-state logic types.
Q05 of 05SENIOR
Why is it recommended to overload operators on structs rather than classes?
ANSWER
Operators on structs are naturally immutable — they return new instances rather than modifying the original. This matches the mathematical semantics of operators: a + b produces a new value. On a class, if you define operator+ to return a new instance, compound assignment a += b silently replaces the reference, which confuses developers who expect in-place mutation (a common mistake). Additionally, value types like Money, Vector, and Temperature are conceptually values, not entities. Using a readonly struct enforces immutability and ensures operator semantics are correct by design. If you must use a class, document the behavior clearly and make the class immutable.
01
Why does C# require you to overload == and != together, and what happens if you override Equals without also overriding GetHashCode?
SENIOR
02
What is the difference between implicit and explicit conversion operators in C#? Give a concrete example of when you'd choose each.
SENIOR
03
Can you overload the += operator in C#? How does compound assignment actually work for overloaded types?
SENIOR
04
Explain how the true and false operators enable short-circuit && and || in C#.
SENIOR
05
Why is it recommended to overload operators on structs rather than classes?
SENIOR
FAQ · 6 QUESTIONS
Frequently Asked Questions
01
Can you overload all operators in C#?
No. C# allows overloading of arithmetic, bitwise, comparison, and unary operators, but deliberately blocks assignment (=), conditional logic (&&, ||), and flow-control operators (new, typeof, is, as). The compound assignment operators like += are also blocked because C# derives them automatically from the base operator, ensuring a + b and a += b are always consistent.
Was this helpful?
02
Should I overload operators on a class or a struct in C#?
Prefer struct for types that model a value — Money, Vector, Temperature, Percentage. Operators on structs naturally return new values rather than mutating state, which matches how arithmetic operators feel to callers. On a class, operator+ returning a new instance can confuse developers who expect in-place mutation, especially with compound assignment. If your type is inherently reference-based (e.g. a mutable entity with identity), stick to regular methods.
Was this helpful?
03
Does overloading == in C# replace the Object.Equals method?
No, and this is a common source of bugs. The == operator and Object.Equals are separate mechanisms. The == operator is a static method resolved at compile time based on the declared type of the variables. Equals is a virtual method resolved at runtime. If you overload == without overriding Equals, types stored in object variables, passed to collections, or compared via LINQ will still use the default reference-equality Equals. Always override both together.
Was this helpful?
04
Can I overload the true and false operators for any type?
Yes, but they are rarely needed. They are used to enable short-circuit && and || with your custom type. You must define both true and false operators as a pair, along with the & and | operators. This is an advanced pattern useful for nullable or tri-state logic types. Most business types do not need it.
Was this helpful?
05
What happens if I define operator+ but not operator-?
The compiler will not complain — it is perfectly valid to define only addition without subtraction. However, callers will expect symmetry for numeric-like types. If your type is meant to represent a quantity, consider defining the full set of arithmetic operators (+, -, *, /) that make sense for the domain. For types where subtraction is not meaningful (e.g., a Temperature struct where subtracting two temperatures gives a temperature difference, not another temperature), you can omit it, but document the decision.
Was this helpful?
06
Do implicit conversions cause performance overhead?
Usually not. The compiler inlines conversion methods just like any other method. However, if the conversion is expensive (e.g., involves a database call or complex computation), it will be a hidden cost because the caller does not see a function call. Keep conversions cheap. If the operation is expensive, make it an explicit conversion or a named method so the caller is aware of the cost.