C# 14: User-Defined Compound Assignment Operators
Introduction
C# 14 brings an interesting upgrade to operator overloading: custom types can now define their own compound assignment operators, such as +=, -=, *=, or /=.
Until now, you could only overload the individual operators (+, –, etc.), and the compiler would synthesize the compound version for you. With this new feature, you gain full control over how a compound operation behaves, especially useful when the “in-place” semantics matter.
How it works in practice
Previously, defining operator + automatically provided +=, but its behaviour was fixed: C# simply rewrote it as x = x + y.
That’s fine for simple immutable objects, but not ideal when a compound operation should mutate the instance or apply more complex rules.
C# 14 lets you write:
This unlocks richer behaviours for types like money, vectors, counters, timers, damage values, or any domain model requiring more than simple arithmetic.
Benefits in real-world scenarios
- Cleaner and more intuitive APIs
Users of your type get predictable operator behaviour that aligns with the type’s intended meaning. - Domain-specific behaviour feels natural
+= can represent business logic rather than just arithmetic, for example applying bonuses, multipliers, thresholds, or conversions. - Performance gains for value types
Being able to mutate a struct in place can reduce allocations and copies, something the automatic rewritex = x + ycould not optimize.
Considerations
- You still need the corresponding base operator (+, –, etc.) alongside the compound one.
- Keep the semantics of += consistent and unsurprising for consumers of your API.
- When using mutable structs, remember the usual caveats about copies and unintended mutations.
Conclusion
User-defined compound assignment operators in C# 14 bring welcome flexibility to custom types. Whether you’re modelling domain concepts, optimizing struct behavior, or creating expressive APIs, this feature gives you precise control over how “in-place” operations should behave, turning operator overloads into a more powerful design tool.