C operator overload for ?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, you do not overload += directly for your own type. Instead, you overload +, and the compiler rewrites a += b as a = a + b for normal user-defined types. Understanding that rewrite is the key to getting predictable behavior.
Overload +, Not +=
The language only lets you define specific operators, and compound assignment is not one of them. If your type supports addition, define operator + and let the compiler use it for +=.
This works because the compiler expands the compound assignment into a regular addition followed by an assignment back to value.
Prefer Immutable Semantics
Operator overloads are easiest to reason about when they behave like built-in numeric types. In other words, + should usually produce a new value instead of mutating one operand.
With this design, counterA += counterB creates a new Counter and assigns it back to counterA. That is much easier to understand than secretly mutating counterA inside operator +.
If mutation is the real goal, a named method such as AddInPlace is usually clearer than a surprising operator overload.
Remember Related Operators and Equality
Once a type supports arithmetic operators, people expect the rest of the API to feel consistent. That often means adding equality and useful string output as well.
If you define == and !=, always keep them aligned with Equals and GetHashCode. Otherwise dictionaries, sets, and tests may behave inconsistently.
Choose Operators Only When They Match the Domain
Operator overloading is best when the meaning is obvious. Vectors, complex numbers, matrices, and money types often benefit from + because users already understand the operation.
It is a poor fit when the meaning is non-obvious, expensive, or side-effectful. An overloaded operator should not perform network requests, database writes, or logging. Those behaviors are too surprising for something that looks like arithmetic.
Common Pitfalls
The most common mistake is searching for a way to overload += directly. C# does not let you do that. Implement + instead.
Another problem is mutating the left operand inside operator +. That makes a + b look pure while actually changing a, which violates normal caller expectations.
Developers also forget about null handling for reference types. If your operator works on classes, decide what null + value should mean, or reject it clearly with an exception.
Finally, avoid defining operators only because the language allows it. If a named method explains intent better, use the named method.
Summary
- C# does not support overloading
+=directly for user-defined types. - Implement
operator +, and the compiler will use it for compound assignment. - Prefer immutable operator behavior so
a + breturns a new value. - Keep equality members consistent when you add comparison operators.
- Use operator overloading only when the meaning is obvious and unsurprising.

