Skip to content

Commit 7fde4c8

Browse files
authored
Make static increment and decrement operator immutable
The call to the static operator a++ already do the storing in a of the new value. And doing `++operand.numerator ` will means `b = a++` will have b receive a mutated value and calls to a method with `Console.WriteLine(myFraction++);` would have the new mutated value instead of the old value. So I'm changing the line to operand.numerator + 1;
1 parent 95a015f commit 7fde4c8

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)