Overloading operator== versus Equals()

前端 未结 6 867
生来不讨喜
生来不讨喜 2020-11-29 20:46

I\'m working on a C# project on which, until now, I\'ve used immutable objects and factories to ensure that objects of type Foo can always be compared for equal

6条回答
  •  余生分开走
    2020-11-29 21:15

    For immutable types I don't think there is anything wrong with having == overloaded to support value equality. I don't think I'd override == without overriding Equals to have the same semantics however. If you do override == and need to check reference equality for some reason, you can use Object.ReferenceEquals(a,b).

    See this Microsoft article for some useful guidelines

提交回复
热议问题