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

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

提交回复
热议问题