.NET 10 is scheduled to be released later this year and, with it, comes the next iteration of C#. C# 14 has several cool features planned, such as extension members and field-backed properties. However, one, perhaps lesser-known, feature is the new user-defined compound assignment.
The code snippets in this article are for C# 14, which is still in development. This means that the syntax might change slightly before the final release of .NET 10. However, I will endeavor to keep this article updated if it does.
To run the code snippets yourself, you need to download and install the latest .NET 10 preview SDK. Next, create a new C# project (using a Console project in this article) and ensure it targets .NET 10. You'll also need to enable preview language features by opening your *.csproj file and adding the <LangVersion>preview</LangVersion> tag:
To understand how user-defined compound assignments will work, you should understand how compound assignments currently work in C# 13 and below. Traditionally, compound assignments like x += y are simply shorthand for x = x + y. Classes and structs can override the + operator, and this override is then automatically used when using a compound assignment: