Why can '=' not be overloaded in C#?

前端 未结 12 850
离开以前
离开以前 2020-12-30 00:49

I was wondering, why can\'t I overload \'=\' in C#? Can I get a better explanation?

相关标签:
12条回答
  • 2020-12-30 01:17

    If you overloaded '=' you would never be able to change an object reference after it's been created. ... think about it - any call to theObjectWithOverloadedOperator=something inside the overloaded operator would result in another call to the overloaded operator... so what would the overloaded operator really be doing ? Maybe setting some other properties - or setting the value to a new object (immutability) ? Generally not what '=' implies..

    You can, however, override the implicit & explicit cast operators: http://www.blackwasp.co.uk/CSharpConversionOverload.aspx

    0 讨论(0)
  • 2020-12-30 01:20

    This code is working for me:

    public class Class1
    {
    
        ...
    
        public static implicit operator Class1(Class2 value)
        {
            Class1 result = new Class1();
    
            result.property = value.prop;
    
            return result;
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-30 01:25

    I don't think there's any really particular single reason to point to. Generally, I think the idea goes like this:

    • If your object is a big, complicated object, doing something that isn't assignment with the = operator is probably misleading.

    • If your object is a small object, you may as well make it immutable and return new copies when performing operations on it, so that the assignment operator works the way you expect out of the box (as System.String does.)

    0 讨论(0)
  • 2020-12-30 01:31

    One possible explanation is that you can't do proper reference updates if you overload assignment operator. It would literally screw up semantics because when people would be expecting references to update, your = operator may as well be doing something else entirely. Not very programmer friendly.

    You can use implicit and explicit to/from conversion operators to mitigate some of the seeming shortcomings of not able to overload assignment.

    0 讨论(0)
  • 2020-12-30 01:32

    It's allowed in C++ and if not careful , it can result in a lot of confusion and bug hunting.

    This article explains this in great detail.

    http://www.relisoft.com/book/lang/project/14value.html

    0 讨论(0)
  • 2020-12-30 01:32

    Being able to define special semantics for assignment operations would be useful, but only if such semantics could be applied to all situations where one storage location of a given type was copied to another. Although standard C++ implements such assignment rules, it has the luxury of requiring that all types be defined at compile time. Things get much more complicated when Reflection and and generics are added to the list.

    Presently, the rules in .net specify that a storage location may be set to the default value for its type--regardless of what that type is--by zeroing out all the bytes. They further specify that any storage location can be copied to another of the same type by copying all the bytes. These rules apply to all types, including generics. Given two variables of type KeyValuePair<t1,t2>, the system can copy one to another without having to know anything but the size and alignment requirements of that type. If it were possible for t1, t2, or the type of any field within either of those types, to implement a copy constructor, code which copied one struct instance to another would have to be much more complicated.

    That's not to say that such an ability offer some significant benefits--it's possible that, were a new framework being designed, the benefits of custom value assignment operators and default constructors would exceed the costs. The costs of implementation, however, would be substantial in a new framework, and likely insurmountable for an existing one.

    0 讨论(0)
提交回复
热议问题