C# Operator Overloading Assignment

Comments and Discussions

 
Assume that I have a base class that keeps more than one fields.
When I implement an explicit equal operation, lets say from string to MyClass type, the return will be a new instance and I will loose other attributes of the original instance. For instance:
MyClass x = new MyClass();
x.Length = 2;
x.FieldToOperate = "convert explicitly";

at that time my length will be gone!
The problem have more impact when the instance is given from other domain where some events on it has been initializes (events like Changed).

Do you have any idea to solve this problem?

thx
Converting from one type to another usually involves some loss. When this is the case the conversion operator MUST be and should be fully documented. If there is no loss, depending on the appropriateness, it can be .

You mention that you are thinking in terms of a base class. If so maybe you should be considering a more polymorphic approach so you can convert between base and derived classes where the object supports it?
Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

Good article, this is very good for beginers. Operator overloading has been in C++ for a long time. this is new only to people how never knew c++.
Love the way of your examples.
Thank you for a good article.
Thankyou and you're welcome

Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)


Agree, very nice article. Thanks

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

The title is be misleading, so please read entire question :-).

By "compound assignment operator" I have in mind a construct like this , for example . Pure assignment operator () does not belong to my question.

By "why" I don't mean an opinion, but resource (book, article, etc) when some of the designers, or their coworkers, etc. express their reasoning (i.e. the source of the design choice).

I am puzzled by asymmetry found in C++ and C# (yes, I know C# is not C++ 2.0) -- in C++ you would overload operator and then almost automatically write appropriate operator relying on previously defined operator. In C# it is in reverse -- you overload and is synthesised for you.

If I am not mistaken the later approach kills a chance for optimization in case of actual , because new object has to be created. So there has to be some big advantage of such approach, but MSDN is too shy to tell about it.

And I wonder what that advantage is -- so if you spotted explanation in some C# book, tech-talk video, blog entry, I will be grateful for the reference.

The closest thing I found is a comment on Eric Lippert blog Why are overloaded operators always static in C#? by Tom Brown. If the static overloading was decided first it simply dictates which operators can be overloaded for structs. This further dictates what can be overloaded for classes.

c#language-designoperators

0 thoughts on “C# Operator Overloading Assignment

Leave a Reply

Your email address will not be published. Required fields are marked *