I am sure if I override Equals, I need to override GetHashCode as well to make sure Dictionary, etc. data structures work as expected.
But if I just want to override ToString, do I still have to override Equals and GetHashCode methods?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 already 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: DateTime.ToString Method