Skip to content

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 /=.

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.

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.

public class Counter
{
public int Value { get; private set; }
public void operator +=(int increment) => Value += increment;
}

Usage:

Counter counter = new();
counter += 5;
  • 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.
  • 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.