I am sure if I override Equals, I need to override GetHashCode as well to make sure Dictionary etc.. data structures works as expected.
But if i just want to override ToString, do I still have to override Equals and GetHashCode methods.
Overriding those three methods serve three different purposes:
As you can see 2 and 3 are related, but 1 is separate. Unless you implement Equals to simply test if the ToString of two objects are equals, which would most likely be a mistake. :)
So the short answer has allready been given: You can override ToString without overriding the two other methods. It is quite normal to even overload the ToString method. See DateTime for an example: http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
No, you don't have to override Equals
and GetHashCode
, they're not related to ToString
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