Senior 8 min · March 06, 2026

C# Operator Overloading: No GetHashCode Breaks HashSet

A missing GetHashCode when overloading operator== caused duplicate HashSet entries in payment reconciliation, wasting hours.

N
Naren Founder & Principal Engineer

20+ years shipping production .NET services in enterprise systems. Notes here come from systems that actually shipped.

Follow
Production
production tested
May 24, 2026
last updated
1,554
articles · all by Naren
 ● Production Incident 🔎 Debug Guide ⚙ Triage Commands
Quick Answer
  • 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
using System;

// A simple struct representing a temperature reading
public struct Temperature
{
    public double Celsius { get; }

    public Temperature(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.
    public static Temperature operator +(Temperature left, Temperature right)
    {
        return new Temperature(left.Celsius + right.Celsius);
    }

    // Scalar multiplication: allow 'temperature * factor'
    // One parameter is our type, the other is double — that's allowed.
    public static Temperature operator *(Temperature temp, double factor)
    {
        return new Temperature(temp.Celsius * factor);
    }

    // Override ToString so our output is readable
    public override string ToString() => $"{Celsius:F1}°C";
}

class Program
{
    static void Main()
    {
        var morningTemp = new Temperature(18.5);
        var afternoonRise = new Temperature(6.0);

        // This calls operator+ behind the scenes
        Temperature peakTemp = morningTemp + afternoonRise;
        Console.WriteLine($"Peak temperature: {peakTemp}");

        // This calls operator* — scalar scaling
        Temperature 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.
Operator Overloading and GetHashCode in C# THECODEFORGE.IO Operator Overloading and GetHashCode in C# How missing GetHashCode breaks HashSet with overloaded operators Operator Overloading Defines custom behavior for operators like +, ==, etc. GetHashCode Requirement Must override when overloading equality operators HashSet Relies on Hash Uses GetHashCode for bucket assignment and lookup Missing Override Default GetHashCode leads to incorrect behavior Broken HashSet Duplicates allowed, lookups fail, performance degrades Consistent Override Override GetHashCode and Equals together ⚠ Forgetting GetHashCode when overloading == or != Always override GetHashCode and Equals together THECODEFORGE.IO
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
using System;
using System.Collections.Generic;

public readonly struct Money : IEquatable<Money>
{
    public decimal Amount { get; }
    public string Currency { get; }

    public Money(decimal amount, string currency)
    {
        if (string.IsNullOrWhiteSpace(currency))
            throw new ArgumentException("Currency code cannot be empty.", nameof(currency));

        Amount = Math.Round(amount, 2); // Money is always 2 decimal places
        Currency = currency.ToUpperInvariant();
    }

    // ── Arithmetic operators ────────────────────────────────────────────

    public static Money operator +(Money left, Money right)
    {
        // Business rule lives HERE, not scattered across callers
        EnsureSameCurrency(left, right, "+");
        return new Money(left.Amount + right.Amount, left.Currency);
    }

    public static Money operator -(Money left, Money right)
    {
        EnsureSameCurrency(left, right, "-");
        return new Money(left.Amount - right.Amount, left.Currency);
    }

    // Scale by a factor — e.g. applying a tax rate
    public static Money operator *(Money money, decimal factor)
    {
        return new Money(money.Amount * factor, money.Currency);
    }

    // Allow factor * money as well (commutativity for the caller's convenience)
    public static Money operator *(decimal factor, Money money) => money * factor;

    public static bool operator >(Money left, Money right)
    {
        EnsureSameCurrency(left, right, ">");
        return left.Amount > right.Amount;
    }

    public static bool operator <(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 GetHashCode

    public static bool operator ==(Money left, Money right) => left.Equals(right);
    public static bool operator !=(Money left, Money right) => !left.Equals(right);

    // IEquatable<Money> — used by List.Contains, LINQ, etc. without boxing
    public bool Equals(Money other)
        => Amount == other.Amount &&
           string.Equals(Currency, other.Currency, StringComparison.OrdinalIgnoreCase);

    // Required override when Equals is overridden — used by dictionaries/hashsets
    public override bool Equals(object? obj)
        => obj is Money other && Equals(other);

    public override int GetHashCode()
        => HashCode.Combine(Amount, Currency.ToUpperInvariant());

    public override string ToString() => $"{Currency} {Amount:F2}";

    // ── Private helpers ─────────────────────────────────────────────────

    private static void EnsureSameCurrency(Money left, Money right, string op)
    {
        if (!string.Equals(left.Currency, right.Currency, StringComparison.OrdinalIgnoreCase))
            throw new InvalidOperationException(
                $"Cannot apply '{op}' to {left.Currency} and {right.Currency}. " +
                $"Convert to the same currency first.");
    }
}

class Program
{
    static void Main()
    {
        var price = new Money(19.99m, "USD");
        var shipping = new Money(4.99m, "USD");
        var taxRate = 0.08m; // 8% tax

        Money 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 correctly
        var duplicatePrice = new Money(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 Equals
        var priceSet = new HashSet<Money> { price, shipping, duplicatePrice };
        Console.WriteLine($"HashSet count (price added twice): {priceSet.Count}");
        Console.WriteLine();

        // Comparison operators
        Console.WriteLine($"total > price: {total > price}");

        // This would throw — different currencies
        try
        {
            var euros = new Money(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
using System;

public struct Percentage
{
    public double Value { get; } // 0.0 to 100.0

    public Percentage(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"
    public static Percentage operator !(Percentage p)
        => new Percentage(100.0 - p.Value);

    // Unary increment — nudge up by 1 point
    public static Percentage operator ++(Percentage p)
        => new Percentage(p.Value + 1.0);

    // Binary addition
    public static Percentage operator +(Percentage left, Percentage right)
        => new Percentage(left.Value + right.Value); // clamped by constructor

    // Comparison pair — C# REQUIRES both < and > if you want either
    public static bool operator <(Percentage left, Percentage right)
        => left.Value < right.Value;

    public static bool operator >(Percentage left, Percentage right)
        => left.Value > right.Value;

    // Equality pair — C# REQUIRES both == and != if you want either
    public static bool operator ==(Percentage left, Percentage right)
        => Math.Abs(left.Value - right.Value) < 0.0001; // float-safe comparison

    public static bool operator !=(Percentage left, Percentage right)
        => !(left == right);

    public override bool Equals(object? obj)
        => obj is Percentage other && this == other;

    public override int GetHashCode() => Math.Round(Value, 4).GetHashCode();

    public override string ToString() => $"{Value:F1}%";
}

class Program
{
    static void Main()
    {
        var discount = new Percentage(30.0);
        var extraOff = new Percentage(15.0);

        // += is automatically derived from operator+ — we never wrote it
        Percentage combined = discount;
        combined += extraOff;
        Console.WriteLine($"Combined discount: {combined}");

        // Unary ! gives us the "remaining" percentage
        Percentage customerPays = !combined;
        Console.WriteLine($"Customer pays: {customerPays}");

        // ++ operator
        Percentage bumped = discount;
        bumped++;
        Console.WriteLine($"Bumped discount: {bumped}");

        // Clamping — adding beyond 100% is safe
        var huge = new Percentage(80.0) + new Percentage(80.0);
        Console.WriteLine($"Clamped result: {huge}");

        // Comparison
        Console.WriteLine($"discount > extraOff: {discount > extraOff}");

        // Demonstrating that == and != must be paired
        Console.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
using System;

public readonly struct Celsius
{
    public double Degrees { get; }

    public Celsius(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)
    public static implicit operator Celsius(double degrees)
        => new Celsius(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.
    public static explicit operator double(Celsius celsius)
        => (celsius.Degrees * 9.0 / 5.0) + 32.0;

    // Arithmetic still works — combining with previous section's lessons
    public static Celsius operator +(Celsius left, Celsius right)
        => new Celsius(left.Degrees + right.Degrees);

    public override string ToString() => $"{Degrees:F2}°C";
}

// A lightweight struct that ONLY holds Fahrenheit — shows cross-type conversion
public readonly struct Fahrenheit
{
    public double Degrees { get; }
    public Fahrenheit(double degrees) => Degrees = degrees;

    // Explicit conversion FROM Celsius TO Fahrenheit
    public static explicit operator Fahrenheit(Celsius c)
        => new Fahrenheit((c.Degrees * 9.0 / 5.0) + 32.0);

    // Explicit conversion back from Fahrenheit to Celsius
    public static explicit operator Celsius(Fahrenheit f)
        => new Celsius((f.Degrees - 32.0) * 5.0 / 9.0);

    public override string ToString() => $"{Degrees:F2}°F";
}

class Program
{
    static void Main()
    {
        // Implicit conversion — no cast syntax required
        Celsius boiling = 100.0;  // implicit operator Celsius(double) fires here
        Console.WriteLine($"Boiling point: {boiling}");

        // Explicit conversion to double — caller must acknowledge scale change
        double boilingF = (double)boiling;
        Console.WriteLine($"In Fahrenheit (as double): {boilingF:F2}");

        // Explicit conversion to Fahrenheit struct
        Fahrenheit boilingFahrenheit = (Fahrenheit)boiling;
        Console.WriteLine($"In Fahrenheit (as struct): {boilingFahrenheit}");

        // Round-trip: Fahrenheit → Celsius → back to Fahrenheit
        var bodyTemp = new Fahrenheit(98.6);
        Celsius bodyTempC = (Celsius)bodyTemp;
        Fahrenheit roundTrip = (Fahrenheit)bodyTempC;
        Console.WriteLine($"Body temp round-trip: {bodyTemp} → {bodyTempC} → {roundTrip}");

        // Arithmetic on Celsius still works naturally
        Celsius morning = 15.0;   // implicit
        Celsius afternoon = 8.0;  // implicit
        Celsius 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
using System;

// Models a three-state logical value: True, False, Indeterminate
public struct ThreeState
{
    // Internal representation:
    //  1 = True, -1 = False, 0 = Indeterminate
    private readonly sbyte _value;

    private ThreeState(sbyte value) => _value = value;

    public static readonly ThreeState True = new ThreeState(1);
    public static readonly ThreeState False = new ThreeState(-1);
    public static readonly ThreeState Indeterminate = new ThreeState(0);

    // The 'true' operator: returns true if the value is definitely true
    public static bool operator true(ThreeState x) => x._value == 1;

    // The 'false' operator: returns true if the value is definitely false
    public static bool operator false(ThreeState x) => x._value == -1;

    // Logical AND: & operator (needed before && can be used)
    public static ThreeState operator &(ThreeState left, ThreeState right)
    {
        // If either is false, result is false
        if (left._value == -1 || right._value == -1)
            return False;
        // If either is indeterminate, result is indeterminate
        if (left._value == 0 || right._value == 0)
            return Indeterminate;
        // Both are true
        return True;
    }

    // Logical OR: | operator (needed before || can be used)
    public static ThreeState operator |(ThreeState left, ThreeState right)
    {
        // If either is true, result is true
        if (left._value == 1 || right._value == 1)
            return True;
        // If either is indeterminate, result is indeterminate
        if (left._value == 0 || right._value == 0)
            return Indeterminate;
        // Both are false
        return False;
    }

    // Logical NOT: ! operator
    public static ThreeState operator !(ThreeState x)
    {
        return x._value switch
        {
            1 => False,
            -1 => True,
            _ => Indeterminate
        };
    }

    // Override Equals/GetHashCode for consistency
    public override bool Equals(object? obj) => obj is ThreeState other && _value == other._value;
    public override int GetHashCode() => _value.GetHashCode();

    public override string ToString() => _value switch
    {
        1 => "True",
        -1 => "False",
        _ => "Indeterminate"
    };
}

class Program
{
    static void Main()
    {
        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 false
        Console.WriteLine($"t && f: {t && f}");  // False
        Console.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}");  // True
        Console.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 = Indeterminate
        Console.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.

MoneyWithBinaryOperators.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
// io.thecodeforge — csharp tutorial

public readonly struct Money
{
    public decimal Amount { get; }
    public string Currency { get; }

    public Money(decimal amount, string currency)
    {
        Amount = amount;
        Currency = currency ?? throw new ArgumentNullException(nameof(currency));
    }

    public static Money operator +(Money left, Money right)
    {
        if (left.Currency != right.Currency)
            throw new InvalidOperationException($"Currency mismatch: {left.Currency} vs {right.Currency}");
        return new Money(left.Amount + right.Amount, left.Currency);
    }

    public static Money operator *(Money money, decimal multiplier)
    {
        return new Money(money.Amount * multiplier, money.Currency);
    }
}

// Usage:
var salary = new Money(5000, "USD");
var bonus = new Money(1000, "USD");
var total = salary + bonus;
var doubled = salary * 2;
Console.WriteLine(total.Amount);  // 6000
Console.WriteLine(doubled.Amount); // 10000
Output
6000
10000
Production Trap:
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 tutorial

public readonly struct Counter
{
    public int Value { get; }

    public Counter(int value) => Value = value;

    // One method handles both prefix (++x) and postfix (x++)
    public static Counter operator ++(Counter c) => new Counter(c.Value + 1);

    // Overload ! to mean "is zero" — domain-specific
    public static bool operator !(Counter c) => c.Value == 0;
}

// Usage:
var c = new Counter(5);
c++;               // postfix: returns old value (5), c becomes 6
++c;               // prefix: returns new value (7), c becomes 7
Console.WriteLine(c.Value);  // 7
Console.WriteLine(!c);       // False (Value is not 0)

var zero = new Counter(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 tutorial

public readonly struct Money
{
    private readonly decimal _amount;
    private readonly string _currency;

    public Money(decimal amount, string currency)
    {
        _amount = amount;
        _currency = currency;
    }

    // Safe — no data loss
    public static implicit operator decimal(Money m) => m._amount;

    // Risky — we drop currency and may round
    public static explicit operator Money(decimal d) =>
        new Money(Math.Round(d, 2), "USD");
}

var price = new Money(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.
// Explicit conversion prevents silent truncation.
Production Trap:
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 tutorial

public readonly struct Validated<T>
{
    private readonly T _value;
    private readonly bool _isValid;

    public Validated(T value, bool isValid)
    {
        _value = value;
        _isValid = isValid;
    }

    public static bool operator true(Validated<T> v) => v._isValid;
    public static bool operator false(Validated<T> v) => !v._isValid;

    // Required for && to work
    public static Validated<T> operator &(Validated<T> a, Validated<T> b) =>
        new Validated<T>(a._isValid ? b._value : a._value, a._isValid && b._isValid);
}

var a = new Validated<int>(42, true);
var b = new Validated<int>(0, false);

if (a && b) Console.WriteLine("Both valid");
else Console.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.
Commands
Inspect the type: `typeof(MyType).GetMethods() | Where-Object { $_.Name -match 'GetHashCode|Equals' }`
Check if GetHashCode uses all fields: `HashCode.Combine(Field1, Field2)`
Fix now
Add GetHashCode override using HashCode.Combine.
Compile error: 'Operator == requires matching operator !='+
Immediate action
Read the exact error — it tells you which operator is missing.
Commands
Search for `operator ==` in file, then add `public static bool operator !=(...) => !(...left.Equals(right));`
Verify the pair compiles: `csc /target:library MyFile.cs`
Fix now
Add the missing operator overload.
Implicit conversion caused unexpected method resolution+
Immediate action
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
AspectOperator OverloadingRegular Methods
Syntax at call siteprice + tax (natural, concise)price.Add(tax) (verbose, method-flavoured)
DiscoverabilityRelies on docs or IDE hints — not obvious a '+' is definedShows up in IntelliSense autocomplete immediately
Best fit forValue 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 extraMust write AddInPlace or similar method manually
InteroperabilityOther CLR languages call op_Addition by name — works but uglyUniversally callable from any CLR language using standard syntax
Enforcement of symmetryC# forces you to define == and != together, < and > togetherNo automatic pairing — easy to forget the inverse method
Risk of misuseCan create confusing code if semantics don't match the symbolMethod name is always explicit — harder to mislead
PerformanceInlined by JIT — effectively zero overhead for simple struct operationsSame — 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.
FAQ · 6 QUESTIONS

Frequently Asked Questions

01
Can you overload all operators in C#?
02
Should I overload operators on a class or a struct in C#?
03
Does overloading == in C# replace the Object.Equals method?
04
Can I overload the true and false operators for any type?
05
What happens if I define operator+ but not operator-?
06
Do implicit conversions cause performance overhead?
N
Naren Founder & Principal Engineer

20+ years shipping production .NET services in enterprise systems. Notes here come from systems that actually shipped.

Follow
Verified
production tested
May 24, 2026
last updated
1,554
articles · all by Naren
🔥

That's OOP in C#. Mark it forged?

8 min read · try the examples if you haven't

Previous
Generics in C#
8 / 10 · OOP in C#
Next
Indexers in C#