C# pre- and post-increment operator overloading how-to?
There is a differen开发者_高级运维ce between h = ++i
and h = i++
in C#. So what I need is a way to declare different overloaded operators for pre-increment (i.e. ++i) and post-increment (i++) in C#. How would I do that please?
I am aware of the fact that both operators do the same to the value they work on - the problem is the point of time when the assignment is done. I know how to do it in C++, but that is not the question. I am wondering whether there is a way to overload both ways this operator can behave in C#, and if so, how to do it.
You can't http://msdn.microsoft.com/en-us/library/36x43w8w(v=vs.71).aspx. You can only overload ++, the only difference is when your overload will be called.
Edit: Also, you can only overload ++ for user-defined types, so it's basically just becoming an alias for a function of your choice.
There is no difference in decleration of those operators. Infact, they act exactly the same, the only difference is the returned value. The ++
operator overload works for both, the different return value is supplied by the runtime, not the user code.
Edit: found it What is the difference between i++ and ++i?
精彩评论