Skip to content

Add reference and fundamentals for user defined compound assignment operators #46674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 12, 2025

Conversation

BillWagner
Copy link
Member

@BillWagner BillWagner commented Jun 10, 2025

Fixes #46100

  • Publish the new speclet.
  • Add text in "What's new in C# 14".
  • Add updates in the .NET 10 What's new article.
  • Update all relevant operator pages to change the language on overloading the associated compound assignment operators.

Internal previews

Toggle expand/collapse
📄 File 🔗 Preview link
docs/core/whats-new/dotnet-10/overview.md What's new in .NET 10
docs/csharp/language-reference/operators/addition-operator.md Addition operators - + and +=
docs/csharp/language-reference/operators/arithmetic-operators.md Arithmetic operators (C# reference)
docs/csharp/language-reference/operators/assignment-operator.md "Assignment operators - assign an expression to a variable"
docs/csharp/language-reference/operators/bitwise-and-shift-operators.md Bitwise and shift operators (C# reference)
docs/csharp/language-reference/operators/boolean-logical-operators.md docs/csharp/language-reference/operators/boolean-logical-operators
docs/csharp/language-reference/operators/operator-overloading.md Operator overloading - predefined unary, arithmetic, equality, and comparison operators
docs/csharp/language-reference/operators/subtraction-operator.md "- and -= operators - subtraction (minus) operators"
docs/csharp/programming-guide/classes-and-structs/access-modifiers.md Access Modifiers (C# Programming Guide)
docs/csharp/programming-guide/classes-and-structs/members.md docs/csharp/programming-guide/classes-and-structs/members
docs/csharp/specification/toc.yml docs/csharp/specification/toc
docs/csharp/whats-new/csharp-14.md What's new in C# 14

Add the speclet to the docset.
Add text in What's new in .NET 10 and What's new in C# 14.
This commit updates the files for operators where compound assignment can be overloaded.
@BillWagner BillWagner marked this pull request as ready for review June 12, 2025 15:02
@BillWagner BillWagner requested a review from a team as a code owner June 12, 2025 15:02
@BillWagner BillWagner requested a review from gewarren June 12, 2025 15:03
Copy link
Member Author

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments for reviewers.

@BillWagner BillWagner requested a review from Copilot June 12, 2025 15:06
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support and documentation for user-defined compound assignment, increment, and decrement operators in C# 14 across spec, language reference, and “What’s new” articles. Key changes include:

  • Inclusion of a new speclet link and TOC entry for user-defined compound assignment.
  • Updates to operator reference topics to describe explicit overloading rules for compound assignment, ++, and --.
  • Addition of code snippets showing instance-based and void-returning compound operator implementations.

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
docs/csharp/whats-new/csharp-14.md Added “User defined compound assignment” section
docs/csharp/specification/toc.yml Inserted TOC entry for user-defined compound assignment
docs/csharp/programming-guide/classes-and-structs/members.md Modified operator member description
docs/csharp/programming-guide/classes-and-structs/access-modifiers.md Updated access rules for user-defined operators
docs/csharp/language-reference/operators/subtraction-operator.md Refreshed dates and overloadability text
docs/csharp/language-reference/operators/snippets/shared/OperatorOverloading.cs Expanded struct with new compound/increment operators
docs/csharp/language-reference/operators/operator-overloading.md Added compound assignment overload rules
docs/csharp/language-reference/operators/boolean-logical-operators.md Updated code tags and overloadability text
docs/csharp/language-reference/operators/bitwise-and-shift-operators.md Refreshed dates, wording, overloadability text
docs/csharp/language-reference/operators/assignment-operator.md Refreshed dates and compound assignment note
docs/csharp/language-reference/operators/arithmetic-operators.md Refreshed dates, wording, overloadability text
docs/csharp/language-reference/operators/addition-operator.md Refreshed dates, wording, overloadability text
docs/core/whats-new/dotnet-10/overview.md Added bullets for compound, increment, decrement support
docfx.json Registered the new proposal in build configuration
Comments suppressed due to low confidence (4)

docs/csharp/language-reference/operators/snippets/shared/OperatorOverloading.cs:38

  • Using operand.numerator++ returns the original value and mutates only the local copy. Replace with operand.numerator + 1 to produce the correctly incremented numerator.
public static Fraction operator ++(Fraction operand) => new Fraction(operand.numerator++, operand.denominator);

docs/csharp/language-reference/operators/snippets/shared/OperatorOverloading.cs:41

  • Using operand.numerator-- returns the original value and mutates only the local copy. Replace with operand.numerator - 1 to produce the correctly decremented numerator.
public static Fraction operator --(Fraction operand) => new Fraction(operand.numerator--, operand.denominator);

docs/csharp/programming-guide/classes-and-structs/members.md:23

  • Regular operator overloads must include the static modifier. Restore the wording to 'public static method' to match actual C# requirements.
|[Operators](../../language-reference/operators/index.md)|Overloaded operators are considered type members. When you overload an operator, you define it as a public method in a type. For more information, see [Operator overloading](../../language-reference/operators/operator-overloading.md).|

docs/csharp/programming-guide/classes-and-structs/access-modifiers.md:68

  • The rule should require both public and static for regular operators. Update the line to reflect that user-defined operators must be public static.
User-defined operators must always be declared as `public`. For more information, see [Operator overloading](../../language-reference/operators/operator-overloading.md).

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
BillWagner and others added 2 commits June 12, 2025 12:57
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
@BillWagner BillWagner merged commit 6c94fe4 into dotnet:main Jun 12, 2025
8 checks passed
@BillWagner BillWagner deleted the compound-assignment branch June 12, 2025 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[C# 14-Fundamentals and reference]: New Feature - User defined compound assignment operators
2 participants