Is there a workaround for overloading the assignment operator in C#?

后端 未结 7 1780
悲哀的现实
悲哀的现实 2020-12-13 02:43

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 h

相关标签:
7条回答
  • 2020-12-13 03:02

    An earlier post suggested this:

    public static implicit operator Foo(string normalString) { }

    I tried this approach... but to make it work you need this:

    public static implicit operator Foo(Foo original) { }

    and the compiler won't let you have an implicit conversion function from your exact type, nor from any base type of yourself. That makes sense since it would be a backdoor way of overriding the assignment operator, which C# doesn't want to allow.

    0 讨论(0)
  • 2020-12-13 03:02

    Maybe what you're looking for can be solved using C# accessors.

    http://msdn.microsoft.com/en-us/library/aa287786(v=vs.71).aspx

    0 讨论(0)
  • 2020-12-13 03:05

    Here is a solution that worked for myself :

    public class MyTestClass
    {
       private int a;
       private string str;
    
       public MyTestClass()
       {
          a = 0;
          str = null;
       }
    
       public MyTestClass(int a, string str)
       {
          this.a = a;
          this.str = str;
       }
    
       public MyTestClass Clone
       {
          get
          {
             return new MyTestClass(this.a, this.str);
          }
       }
    }
    

    Somewhere else in the code :

    MyTestClass test1 = new MyTestClass(5, "Cat");
    MyTestClass test2 = test1.Clone;
    
    0 讨论(0)
  • 2020-12-13 03:17

    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";
    
    0 讨论(0)
  • 2020-12-13 03:17

    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;

    0 讨论(0)
  • 2020-12-13 03:26

    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.)
    0 讨论(0)
提交回复
热议问题