Skip to content

Commit adba9e0

Browse files
committed
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
1 parent a574d62 commit adba9e0

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public Fraction(int numerator, int denominator)
3636

3737
// Define increment and decrement to add 1/den, rather than 1/1.
3838
public static Fraction operator ++(Fraction operand)
39-
=> new Fraction(operand.numerator++, operand.denominator);
39+
=> new Fraction(operand.numerator + 1, operand.denominator);
4040

4141
public static Fraction operator --(Fraction operand) =>
42-
new Fraction(operand.numerator--, operand.denominator);
42+
new Fraction(operand.numerator - 1, operand.denominator);
4343

4444
public override string ToString() => $"{numerator} / {denominator}";
4545

0 commit comments

Comments
 (0)