I need to override Equals() method for one of my types but it seems I have to also override GetHashCode() method.
I am not sure:
If I have type Animal and if I have 2 instances of Animal which are basically the same(equal)Cats; like:
Animal cat_01 = new Animal("Kitty", "Pink");
Animal cat_02 = new Animal("Kitty", "Pink");
Should I implement the GetHashedCode() to retirn same value for both cas_01 and cat_02 eventhough they represent different references in the memory?
Is it the way GetHashCode() shuold work?
Thanks
MSDN says:
If two objects compare as equal, the GetHashCode method for each object must return the same value.
So yes, GetHashCode should return the same value for both instances.
You can still use Object.ReferenceEquals if you want to see if they refer to the same object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With