Overloading operator== versus Equals()

前端 未结 6 856
生来不讨喜
生来不讨喜 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条回答
  •  旧时难觅i
    2020-11-29 21:01

    I believe the standard is that for most types, .Equals checks object similarity, and operator == checks reference equality.

    I believe best practice is that for immutable types, operator == should be checking for similarity, as well as .Equals. And if you want to know if they really are the same object, use .ReferenceEquals. See the C# String class for an example of this.

提交回复
热议问题