From adba9e034c6fd5aad23ce9f8680f0ec315f574f1 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Zeller Date: Fri, 19 Sep 2025 18:57:03 +0200 Subject: [PATCH] Fix increment and decrement static operator The increment and decrement static operator currently are no-op. This fix ensure they actually do increment and decrement the value of the numerator in the fraction. Change the operator from an increment to a non mutating operand.numerator + 1 because otherwise the call to a reference version of Fraction would result in `Console.WriteLine(a++);` to print a after its mutation instead of the before value. Fixes #48626 --- .../operators/snippets/shared/OperatorOverloading.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/language-reference/operators/snippets/shared/OperatorOverloading.cs b/docs/csharp/language-reference/operators/snippets/shared/OperatorOverloading.cs index 643b2cbab8f03..b808fef89c7b4 100644 --- a/docs/csharp/language-reference/operators/snippets/shared/OperatorOverloading.cs +++ b/docs/csharp/language-reference/operators/snippets/shared/OperatorOverloading.cs @@ -36,10 +36,10 @@ public Fraction(int numerator, int denominator) // Define increment and decrement to add 1/den, rather than 1/1. public static Fraction operator ++(Fraction operand) - => new Fraction(operand.numerator++, operand.denominator); + => new Fraction(operand.numerator + 1, operand.denominator); public static Fraction operator --(Fraction operand) => - new Fraction(operand.numerator--, operand.denominator); + new Fraction(operand.numerator - 1, operand.denominator); public override string ToString() => $"{numerator} / {denominator}";