How do we implement the overload for postfix and prefix operators in c#
void Main()
{
MyClass myclass=new MyClass();
myclass.x=5;
Console.WriteLine((++myclass).x);
Console.WriteLine((myclass++).x);
}
public class MyClass
{
public int x;
public static MyClass operator ++(MyClass m)
{
m.x=m.x+1;
return m;
}
}
this might be an unnecessary operator overload, but its known that ++ operator can be overloaded. How do we achieve the different behaviour here ( i++, ++i)
From what I've seen, overloading the unary operator ++ in C# overloads both the postfix and prefix versions of the operator.
sources: http://devhawk.net/2003/07/09/operator-overloading-in-c/ http://www.programmingvideotutorials.com/csharp/csharp-operator-overloading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With