0

I need to compare the values of some reference types that I've created. Which should I override, the Object.Equals method or the == operator? I'm guessing that Object.Equals is the right way to go since I'm not comparing the references themselves.

HelloWorld
  • 2,375
  • 5
  • 22
  • 21
  • possible duplicate of [Correct way to override Equals() and GetHashCode()](http://stackoverflow.com/questions/9317582/correct-way-to-override-equals-and-gethashcode) – Parimal Raj Apr 25 '14 at 03:26

2 Answers2

1

You should override the Equals() method (and most likely GetHashCode() method as well, to preserve the equivalence)

public override bool Equals(object obj) { ... }

public override int GetHashCode() { ... }

Please see the msdn reference on the topic

Also see this possible duplicate

Community
  • 1
  • 1
quantdev
  • 23,517
  • 5
  • 55
  • 88
1

To go along with what karim said, I recommend you follow the MSDN guidelines for overloading the Equals operator, as well as overload the == operator. This gives you a bit more flexibility on your future usage.

GEEF
  • 1,175
  • 5
  • 17