Possible duplicate:
Operator overload with subsequent increase
Why are Postfix ++ / - classified as primary operators in C #?
I saw that I can overload the ++ and -- operators. You usually use these operators in two ways. Pre and post increment / deccrement int Example:
int b = 2; //if i write this Console.WriteLine(++b); //it outputs 3 //or if i write this Console.WriteLine(b++); //outpusts 2
But the situation is slightly different with operator overloading:
class Fly { private string Status { get; set; } public Fly() { Status = "landed"; } public override string ToString() { return "This fly is " + Status; } public static Fly operator ++(Fly fly) { fly.Status = "flying"; return fly; } } static void Main(string[] args) { Fly foo = new Fly(); Console.WriteLine(foo++);
My question is: why do these last two lines output the same thing? And more specifically, why does the first line (of two) print flying ?
Decisions should change the operator overload to:
public static Fly operator ++(Fly fly) { Fly result = new Fly {Status = "flying"}; return result; }
Bosak source share