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

前端 未结 12 896
离开以前
离开以前 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

提交回复
热议问题