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

后端 未结 7 1796
悲哀的现实
悲哀的现实 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.

提交回复
热议问题