-
Notifications
You must be signed in to change notification settings - Fork 6k
Closed
Labels
Description
Type of issue
Code doesn't work
Description
The static operator from the code snippet from snippets/shared/OperatorOverloading.cs do not work properly and instead do nothing to the current "Fraction" struct and is functionally a no-op,
// Define increment and decrement to add 1/den, rather than 1/1.
public static Fraction operator ++(Fraction operand)
// the next line return a fraction with the current numerator.
=> new Fraction(operand.numerator++, operand.denominator);
public static Fraction operator --(Fraction operand) =>
// the next line return a fraction with the current numerator.
new Fraction(operand.numerator--, operand.denominator);
And because Fraction is a struct and not a class. Only the copied fraction has its numerator increased. Not the numerator in the line a++;
so again it's a no-op.
If you run b = ++a; Console.WriteLine(a); Console.WriteLine(b);
in the snippet you'll see that a hasn't changed at all.
Page URL
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/operator-overloading
Content source URL
Document Version Independent Id
5ec5c397-c7c6-700c-bc36-a8dfb2396dad
Platform Id
e83aae5a-e070-c00c-3914-cadfa33a9fe9
Article author
Metadata
- ID: f91d85ae-8b37-e837-a031-144571362fbb
- PlatformId: e83aae5a-e070-c00c-3914-cadfa33a9fe9
- Service: dotnet-csharp
- Sub-service: lang-reference