C# Feature - User-defined compound assignment operators
User-defined compound assignment operators
Section titled “User-defined compound assignment operators”C# 14 allows user-defined types to explicitly overload compound assignment operators such as +=, -=, *=, and /=.
Introduced In
Section titled “Introduced In”This feature was introduced in C# 14. The official Microsoft Learn page for C# 14 is dated November 18, 2025 and states that C# 14 ships with .NET 10 and Visual Studio 2026.
Why This Feature Exists
Section titled “Why This Feature Exists”Before C# 14, compound assignment on user-defined types relied on the corresponding binary operator and then assigned the result back. That behavior worked, but it could allocate or copy unnecessarily. Explicit compound assignment operators let a type implement a more efficient in-place update.
Example
Section titled “Example”public class Counter{ public int Value { get; private set; }
public void operator +=(int increment) => Value += increment;}Usage:
Counter counter = new();counter += 5;When To Use It
Section titled “When To Use It”- Use it for mutable user-defined types where in-place updates are intentional.
- Use it when explicit compound behavior is measurably better than composing binary operators.
Common Pitfalls
Section titled “Common Pitfalls”- This feature makes most sense for carefully designed mutable types. It is usually the wrong fit for immutable value objects.
- Operator overloading should improve clarity, not create clever but opaque APIs.