Assignment operator can NOT be overloaded !

C

Christian

Hi,

operator=() does not exist in C# ?

How then can I implement a custom assignment between 2 objects of type say
MyClass ?

Thnx
Chris
 
N

Nicholas Paldino [.NET/C# MVP]

Christian,

You can't. The reason for this is that the designers wanted the rules
for assignment to be consistent throughout. You will have to create a
method which is called which will do what you want (and I think is more
readable as well).

Hope this helps.
 
J

Jon Skeet [C# MVP]

Christian said:
operator=() does not exist in C# ?

No, thank goodness.
How then can I implement a custom assignment between 2 objects of type say
MyClass ?

You can give implicit conversion operators if you must - but personally
I'd avoid them, and either make the conversion operator explicit or (as
Nicholas suggested) use a method instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top