As the title states, why any object should inherit the ToString() method (in C# or Java for example) and in some way care to convert it to String? Isn't this, in some cases, a violation of the Single Responsability Principle? I mean, if your object doesn't need to be converted to string you will end up having more responsabilities for your object.

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

I disagree that all classes should define the toString() method. Personally, when writing a public API, I define a toString() method in all classes for the sake of other developers, but when developing internals, I only define it if necessary, therefore following SRP.
– FThompsonMay 3 '13 at 15:34

Does an object violate SRP by defining its very own particular way of being represented as a String when required? IMHO it's not about SRP per se, but about an object's required features.
– FritzMay 3 '13 at 15:34

@Vulcan I was not saying that any objects have to redefine the ToString(), but anyway they must have that method even if a specific sub-type shouldn't have the responsability to "convert" its value to string.
– MonesMay 3 '13 at 15:55

1 Answer
1

In a sense, ToString() does violate the single responsibility principle. Converting to a string representation should not necessarily be a requirement of each and every object type. In C#, GetHashCode() is arguably worse - and again, defined on every type. This could easily have been done via some other mechanism (ie: an optional interface, and a class with the single responsibility of converting any object into a string, etc).

It's a matter of practicality vs. correctness. Having a method on every object which provides a string representation (arguably) makes some things simpler overall, at the expense of having each object implement it. That being said, objects do not need to take on this responsibility if they are happy with the default implementation.

Any class object X should be able to determine whether any of its members could ever behave differently from those of the object referred to by Y (any class which implements Equals using reference equality would meet that description, since if X and Y are distinct objects, calling .Equals(X) on X and Y will yield different results). The usefulness of the equivalence relation provided by Equals can be greatly enhanced by having a GetHashCode() method that's consistent with it.
– supercatJul 16 '13 at 19:26

Even if it might make sense semantically to have code that wants to compare objects use a comparison method when one is provided, and test reference equality when one is not, unconditionally calling a virtual method which is known to exist is faster than testing whether an object has a virtual method that it may or may not have].
– supercatJul 16 '13 at 19:32

What do you mean when you say an optional interface - you mean a marker interface? If not, what methods will be in this interface signature?
– BornToCodeDec 24 '15 at 10:28