9

According to MSDN: Most reference types must not overload the equality operator, even if they override Equals. However, if you are implementing a reference type that is intended to have value semantics, such as a complex number type, you must override the equality operator.

What is the best practice to implement equals method and equality operator for a typical domain entity like Customer?

Should it implement equals method to return true if identities of two entities are the same? What if entity is not immutable? What if both entities are new and their identities have empty values. And what about equality operator?

As JaredPar mentioned here Equals will actually measure the equality of the values while == will measure whether or not they are the same reference.

Community
  • 1
  • 1
Jekas
  • 578
  • 4
  • 15
  • 3
    The link you've given doesn't show that text for me - can you clarify where it's coming from? (In particular, it uses the word "override" incorrectly near the end, which raises some suspicion...) – Jon Skeet Nov 10 '11 at 07:27
  • http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx – wRAR Nov 10 '11 at 07:31
  • The direct [link](http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx). See *Notes to Implementers*. – Jekas Nov 10 '11 at 07:33
  • The use of the word "must" is odd here too. Sounds like a poorly expressed guideline with no reasoning behind it - IIRC, "Effective C#" and "C# 4 in a Nutshell" both have more discussion on this. – Jon Skeet Nov 10 '11 at 07:35
  • http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx – Mehran Nov 10 '11 at 07:37
  • there are lots detailed information about the object comparsion :[link]http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.. comparison between objects depends upon their reference and values. have look on this:[link]http://stackoverflow.com/questions/4512444/object-comparison-in-net – Niranjan Singh Nov 10 '11 at 07:44

2 Answers2

5

From MSDN:

Most reference types must not overload the equality operator, even if they override Equals. However, if you are implementing a reference type that is intended to have value semantics, such as a complex number type, you must override the equality operator.

Microsoft thinks that == should be used only for value-like types, e.g. number types such as Complex, BigInt etc. Composite types such as Person should not override the equality operator. It's a matter of code style and Microsoft merly suggests that you follow this guideline. I doubt that the compiled result will be much different.

larsmoa
  • 12,604
  • 8
  • 62
  • 85
4

Typically I won't implement either (= operator or Equals() for my classes, e.g. Customer).

You definately shouldn't override the = operator because developers using your classes expect = to compare the pointers and not the instances themselves, changing this behaviour will just lead to bugs because people don't expect it to work that way.

If you want to include a way to do a semantic comparison that's what the Equals() method is for, and you can override it to implement the equality check in whatever way makes sense for how you wish to use it in your code.

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
  • 1
    Re: _developers using your classes expect == to compare the pointers_. Indeed: the pattern `if (param == null)` is super frequent! – Pablo H May 27 '17 at 00:13