Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the .ToString() method work?

Sometimes when I call a class's .ToString() method, it returns the fully qualified name of the class. But for some class's/struct's (like Int32) it returns a string correspoding to the object (value of the integer). Does this mean the Int32 class overrides the ToString() method, and classes that return fully qualified names don't override it, but instead just call base's (Object's) ToString() method? Does the Object.ToString() implementation just return the class's fully qualified name?

like image 566
deen Avatar asked Apr 09 '12 15:04

deen


People also ask

How does toString method work?

A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.

How do we call toString () method?

By default, the toString() method is called by println, but the method can also be called explicitly on any object. The method can be called on an object, like this – object. toString(), or a numeric value can be passed to the method as an argument, like this – Integer. toString(10).

What does toString () do in JavaScript?

The toString() method returns a string as a string. The toString() method does not change the original string. The toString() method can be used to convert a string object into a string.

What does toString () do in Python?

It returns the string representation of any object. Strings are common features, and each language has its own way of dealing with them. This method is offered by programming languages like Java. Python doesn't have a method named tostring(), but it offers other methods which do the task for you.


2 Answers

Sometimes when I call the ToString method it returns the fully qualified name of the runtime type of the object that received the call.

Correct.

But for some types, such as System.Int32, ToString returns the value of the receiver converted to a string.

Correct.

Does the System.Int32 struct override the ToString method?

Yes.

Do other types whose ToString methods return the fully-qualified type name not override ToString?

That is probably the case, yes. Of course, they could override the method and have the overriding method do exactly the same thing as the base class method, but that would be a bit pointless.

So in those cases, calling ToString just calls the System.Object implementation of ToString, which returns fully qualified name?

Correct.

You seem to have a solid grasp of how this works. My only correction would be to note that System.Int32 is a struct, not a class.

like image 132
Eric Lippert Avatar answered Oct 06 '22 01:10

Eric Lippert


http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx

ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display. (For information about formatting support in the .NET Framework, see Formatting Types.)

The default implementation of the ToString method returns the fully qualified name of the type of the Object, as the following example shows.

Because Object is the base class of all reference types in the .NET Framework, this behavior is inherited by reference types that do not override the ToString method. The following example illustrates this. It defines a class named Object1 that accepts the default implementation of all Object members. Its ToString method returns the object's fully qualified type name.

like image 27
MarcinJuraszek Avatar answered Oct 05 '22 23:10

MarcinJuraszek