views:

4260

answers:

5

Unlike C++, in C# you can't overload the assignment operator.

I'm doing a custom Number class for arithmetic operations with very large numbers and I want it to have the look-and-feel of the built-in numerical types like int, decimal, etc. I've overloaded the arithmetic operators, but the assignment remains...

Is there a workaround for that issue?

A: 

I'm afraid you can't do this.

arul
+5  A: 

You won't be able to work around it having the C++ look, since a = b; has other semantics in C++ than in C#. In C#, a = b; makes a point to the same object like b. In C++, a = b changes the content of a. Both has their ups and downs. It's like you do

MyType * a = new MyType();
MyType * b = new MyType(); 
a = b; /* only exchange pointers. will not change any content */

In C++ (it will lose the reference to the first object, and create a memory leak. But let's ignore that here). You cannot overload the assign operator in C++ for that either.

The workaround is easy:

MyType a = new MyType();
MyType b = new MyType();

// instead of a = b
a.Assign(b);

Disclaimer: I'm not a C# developer

You could create a write-only-property like this. then do a.Self = b; above.

public MyType Self {
    set {
        /* copy content of value to this */
        this.Assign(value);
    }
}

Now, this is not good. Since it violates the principle-of-least-surprise (POLS). One wouldn't expect a to change if one does a.Self = b;

Johannes Schaub - litb
Maybe I could do it that way. But what I meant was - a workaround that gives me the real look-and-feel of assignment operator overloading, so I ca literally say:MyType a = new MyType();MyType b = new MyType();a = b;and it performs some custom assignment logic.
The description of why it doesn't work is spot-on. As you suggest, though, the Self property idea is not so good,
Charlie
+1  A: 

Instead of making a copy of the data when passing the reference you could make the class immutable. When the class is immutable having multiple references to it isn't a problem since it can't be changed.

Operations that changes the data would of course return new instances.

Cristian Libardo
Now that's a good idea! Thanks, I'll try it!
+5  A: 

It's still not at all clear to me that you really need this. Either:

  • Your Number type should be a struct (which is probable - numbers are the most common example of structs). Note that all the types you want your type to act like (int, decimal etc) are structs.

or:

  • Your Number type should be immutable, making every mutation operation return a new instance, in which case you don't need the data to be copied on assignment anyway. (In fact, your type should be immutable whether or not it's a struct. Mutable structs are evil, and a number certainly shouldn't be a mutable reference type.)
Jon Skeet
Yes, maybe the struct idea is good. And I like the immutability option - it was suggested in another answer. Thanks, I'll try what you propose.
+2  A: 

you can use the 'implicit' keyword to create an overload for the assignment:

Suppose you have a type like Foo, that you feel is implicitly convertable from a string. You would write the following static method in your Foo class:

public static implicit operator Foo(string normalString) { //write your code here to go from string to Foo and return the new Foo. }

Having done that, you can then use the following in your code:

Foo x = "whatever";

Cool, this answers a question I was going ot ask ;)
Sam Salisbury
Doesn't that only work if you are trying to assign one type to another? It's an implicit cast, after all. I don't think that would help if you wanted to assign `Number` to `Number`.
Drew Noakes