You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add reference and fundamentals for user defined compound assignment operators (#46674)
* Add speclet
Add the speclet to the docset.
Add text in What's new in .NET 10 and What's new in C# 14.
* Update language reference on operators
This commit updates the files for operators where compound assignment can be overloaded.
* lint
* update operator overloading reference.
* formatting issue
* build issue
* remove outdated restrictions.
* Update docs/csharp/language-reference/operators/operator-overloading.md
* Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Apply suggestions from code review
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
* Respond to feedback.
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
"_csharplang/proposals/partial-events-and-constructors.md": "This proposal allows partial events and constructors to be declared in partial classes. The event and constructor can be split across class declarations.",
816
818
"_csharplang/proposals/null-conditional-assignment.md": "This proposal allows the null conditional operator to be used for the destination of assignment expressions. This allows you to assign a value to a property or field only if the left side is not null.",
817
819
"_csharplang/proposals/extensions.md": "This proposal enables new kinds of extension members. These new extension members support extension properties, extension static members, including extension operators.",
820
+
"_csharplang/proposals/user-defined-compound-assignment.md": "This proposal introduces user-defined compound assignment operators. Developers can override compound assignment, increment, and decrement operators.",
818
821
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "Learn about any breaking changes since the initial release of C# 10 and included in C# 11",
819
822
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "Learn about any breaking changes since the initial release of C# 11 and included in C# 12",
820
823
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "Learn about any breaking changes since the initial release of C# 12 and included in C# 13",
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/operators/addition-operator.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Addition operators - + and +="
3
3
description: "The C# addition operators (`+`, and `+=`) work with operands of numeric, string, or delegate types."
4
-
ms.date: 11/22/2024
4
+
ms.date: 06/11/2025
5
5
f1_keywords:
6
6
- "+_CSharpKeyword"
7
7
- "+=_CSharpKeyword"
@@ -16,19 +16,19 @@ helpviewer_keywords:
16
16
---
17
17
# Addition operators - `+` and `+=`
18
18
19
-
The `+` and `+=` operators are supported by the built-in [integral](../builtin-types/integral-numeric-types.md) and [floating-point](../builtin-types/floating-point-numeric-types.md) numeric types, the [string](../builtin-types/reference-types.md#the-string-type) type, and [delegate](../builtin-types/reference-types.md#the-delegate-type) types.
19
+
The built-in [integral](../builtin-types/integral-numeric-types.md) and [floating-point](../builtin-types/floating-point-numeric-types.md) numeric types, the [string](../builtin-types/reference-types.md#the-string-type) type, and [delegate](../builtin-types/reference-types.md#the-delegate-type) types all support the `+` and `+=` operators.
20
20
21
21
For information about the arithmetic `+` operator, see the [Unary plus and minus operators](arithmetic-operators.md#unary-plus-and-minus-operators) and [Addition operator +](arithmetic-operators.md#addition-operator-) sections of the [Arithmetic operators](arithmetic-operators.md) article.
22
22
23
23
## String concatenation
24
24
25
25
When one or both operands are of type [string](../builtin-types/reference-types.md#the-string-type), the `+` operator concatenates the string representations of its operands (the string representation of `null` is an empty string):
You can use string interpolation to initialize a constant string when all the expressions used for placeholders are also constant strings.
34
34
@@ -38,7 +38,7 @@ Beginning with C# 11, the `+` operator performs string concatenation for UTF-8 l
38
38
39
39
For operands of the same [delegate](../builtin-types/reference-types.md#the-delegate-type) type, the `+` operator returns a new delegate instance that, when invoked, invokes the left-hand operand and then invokes the right-hand operand. If any of the operands is `null`, the `+` operator returns the value of another operand (which also might be `null`). The following example shows how delegates can be combined with the `+` operator:
Those operators are supported by all [integral](../builtin-types/integral-numeric-types.md) and [floating-point](../builtin-types/floating-point-numeric-types.md) numeric types.
41
+
All [integral](../builtin-types/integral-numeric-types.md) and [floating-point](../builtin-types/floating-point-numeric-types.md) numeric types support these operators.
42
42
43
-
In the case of integral types, those operators (except the `++` and `--`operators) are defined for the `int`, `uint`, `long`, and `ulong` types. When operands are of other integral types (`sbyte`, `byte`, `short`, `ushort`, or `char`), their values are converted to the `int` type, which is also the result type of an operation. When operands are of different integral or floating-point types, their values are converted to the closest containing type, if such a type exists. For more information, see the [Numeric promotions](~/_csharpstandard/standard/expressions.md#1247-numeric-promotions) section of the [C# language specification](~/_csharpstandard/standard/README.md). The `++` and `--` operators are defined for all integral and floating-point numeric types and the [char](../builtin-types/char.md) type. The result type of a [compound assignment expression](#compound-assignment) is the type of the left-hand operand.
43
+
The `int`, `uint`, `long`, and `ulong` types define all these operators. The other integral types (`sbyte`, `byte`, `short`, `ushort`, and `char`) define only the `++` and `--` operators. For other operators, when operands are of the integral types `sbyte`, `byte`, `short`, `ushort`, or `char`, their values are converted to the `int` type and the result type is `int`. When operands are of different integral or floating-point types, their values are converted to the closest containing type, if such a type exists. For more information, see the [Numeric promotions](~/_csharpstandard/standard/expressions.md#1247-numeric-promotions) section of the [C# language specification](~/_csharpstandard/standard/README.md). The `++` and `--` operators are defined for all integral and floating-point numeric types and the [char](../builtin-types/char.md) type. The result type of a [compound assignment expression](#compound-assignment) is the type of the left-hand operand.
44
44
45
45
## Increment operator ++
46
46
@@ -114,7 +114,7 @@ For the `float`, `double`, and `decimal` types, the result of the `/` operator i
If one of the operands is `decimal`, another operand can be neither`float` nor `double`, because neither `float` nor `double` is implicitly convertible to `decimal`. You must explicitly convert the `float` or `double` operand to the `decimal` type. For more information about conversions between numeric types, see [Built-in numeric conversions](../builtin-types/numeric-conversions.md).
117
+
If one of the operands is `decimal`, another operand can't be `float` nor `double`, because neither `float` nor `double` is implicitly convertible to `decimal`. You must explicitly convert the `float` or `double` operand to the `decimal` type. For more information about conversions between numeric types, see [Built-in numeric conversions](../builtin-types/numeric-conversions.md).
118
118
119
119
## Remainder operator %
120
120
@@ -136,7 +136,7 @@ For the `float` and `double` operands, the result of `x % y` for the finite `x`
136
136
- The absolute value of `z` is the value produced by `|x| - n * |y|` where `n` is the largest possible integer that is less than or equal to `|x| / |y|` and `|x|` and `|y|` are the absolute values of `x` and `y`, respectively.
137
137
138
138
> [!NOTE]
139
-
> This method of computing the remainder is analogous to that used for integer operands, but different from the IEEE 754 specification. If you need the remainder operation that complies with the IEEE 754 specification, use the <xref:System.Math.IEEERemainder%2A?displayProperty=nameWithType> method.
139
+
> This method of computing the remainder is analogous to the method used for integer operands, but different from the IEEE 754 specification. If you need the remainder operation that complies with the IEEE 754 specification, use the <xref:System.Math.IEEERemainder%2A?displayProperty=nameWithType> method.
140
140
141
141
For information about the behavior of the `%` operator with non-finite operands, see the [Remainder operator](~/_csharpstandard/standard/expressions.md#12104-remainder-operator) section of the [C# language specification](~/_csharpstandard/standard/README.md).
142
142
@@ -170,13 +170,13 @@ For a binary operator `op`, a compound assignment expression of the form
>Theoverflow-checkingcontextwithinthebodyofacheckedoperatorisn't affected by the presence of the `checked` modifier. The default context is defined by the value of the [**CheckForOverflowUnderflow**](../compiler-options/language.md#checkforoverflowunderflow) compiler option. Use the [`checked` and `unchecked` statements](../statements/checked-and-unchecked.md) to explicitly specify the overflow-checking context, as the example at the beginning of this section demonstrates.
275
276
276
277
## C# language specification
277
278
@@ -289,6 +290,7 @@ For more information, see the following sections of the [C# language specificati
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/operators/assignment-operator.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Assignment operators - assign an expression to a variable"
3
3
description: "C# Assignment sets the value of the expression. Alternatively, `ref` assignment sets the reference of a reference variable."
4
-
ms.date: 04/04/2025
4
+
ms.date: 06/11/2025
5
5
f1_keywords:
6
6
- "=_CSharpKeyword"
7
7
helpviewer_keywords:
@@ -77,11 +77,11 @@ You can use the null-coalescing assignment operator `??=` to assign the value of
77
77
78
78
Auser-definedtypecan't [overload](operator-overloading.md) the assignment operator. However, a user-defined type can define an implicit conversion to another type. That way, the value of a user-defined type can be assigned to a variable, a property, or an indexer element of another type. For more information, see [User-defined conversion operators](user-defined-conversion-operators.md).
79
79
80
-
Auser-definedtypecan't explicitly overload a compound assignment operator. However, if a user-defined type overloads a binary operator `op`, the `op=` operator, if it exists, is also implicitly overloaded.
80
+
Ifauser-definedtypeoverloadsabinaryoperator `op`, the `op=` operator, ifitexists, isalsoimplicitlyoverloaded. BeginningwithC# 14, auser-definedtypecanexplicitlyoverloadthecompoundassignmentoperators (`op=`) toprovideamoreefficientimplementation. Typically, atypeoverloadstheseoperatorsbecausethevaluecanbeupdatedinplace, ratherthanallocatinganewinstancetoholdtheresultofthebinaryoperation. Ifatypedoesn't provide an explicit overload, the compiler generates the implicit overload.
0 commit comments