Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does object.ToString() exist?

Tags:

c#

.net

object

Isn't it much more elegant and neat to have an IStringable interface?

Who needs this Type.FullName object returned to us?

EDIT: everyone keeps asking why do I think it's more elegant..

Well, it's just like that, instead of IComparable, object would have CompareTo method, that by default throws an exception or returns 0.

There are objects that cannot and should not be described as a string. object could have equally returned string.Empty. Type.FullName is just an arbitrary choice..

And for methods such as Console.Write(object), I think it should be: Write(IStringable).

However, if you are using WriteLine to anything but strings (or something that its ToString is obvious such as numbers), it seems to me it's for debugging mode only..

By the way - how should I comment to you all? Is it okay that I post an answer?

like image 821
Letterman Avatar asked Oct 13 '09 17:10

Letterman


People also ask

What is the purpose of the toString () method in object Why is it useful?

toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.

Why does string have a toString method?

The java toString() method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String representation of the Object .

Why is toString automatically called?

toString is automatically called for the frog1 Object constructed by the default constructor.

Why do we generate toString in Java?

The toString() method returns the String representation of the object. If you print any object, Java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depending on your implementation.


1 Answers

There are three virtual methods that IMHO should have never been added to System.Object...

  • ToString()
  • GetHashCode()
  • Equals()

All of these could have been implemented as you suggest with an interface. Had they done so I think we'd be much better off. So why are these a problem? Let's just focus on ToString():

  1. If ToString() is expected to be implemented by someone using ToString() and displaying the results you have an implicit contract that the compiler cannot enforce. You assume that ToString() is overloaded, but there is no way to force that to be the case.
  2. With an IStringable you would only need to add that to your generic type-constraint or derive your interface from it to require it's usage on implementing objects.
  3. If the benefit you find in overloading ToString() is for the debugger, you should start using [System.Diagnostics.DebuggerDisplayAttribute].
  4. As for needing this implementation for converting objects to strings via String.Format(), and/or Console.WriteLine, they could have deferred to the System.Convert.ToString(object) and checked for something like 'IStringable', failing over to the type's name if not implemented.
  5. As Christopher Estep points out, it's culture specific.

So I guess I stand alone here saying I hate System.Object and all of it's virtual methods. But I do love C# as a whole and overall I think the designers did a great job.

Note: If you intend to depend upon the behavior of ToString() being overloaded, I would suggest you go ahead and define your IStringable interface. Unfortunatly you'll have to pick another name for the method if you really want to require it.

more

My coworkers and I were just speaking on the topic. I think another big problem with ToString() is answering the question "what is it used for?". Is it Display text? Serialization text? Debugging text? Full type name?

like image 101
csharptest.net Avatar answered Sep 24 '22 00:09

csharptest.net