6

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?

Rauld
  • 910
  • 2
  • 10
  • 18
deen
  • 359
  • 1
  • 3
  • 10

4 Answers4

19

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.

Eric Lippert
  • 612,321
  • 166
  • 1,175
  • 2,033
  • 5
    @Ala: Are you asking me why someone downvoted your question? Probably because it is answered **on the documentation page for the ToString method**. If you have a question about a method, try reading the documentation of that method; if it doesn't answer your question then try searching the web or this site for an answer. Instead of asking people to take time out of their busy day to answer your question, do a basic, minimal level of research yourself first. – Eric Lippert Apr 09 '12 at 16:01
  • I didnt get you. You answered each every line right? This was the answar i expected – deen Apr 09 '12 at 16:03
  • @ Eric, I would like to ask you one personal question? To make use of design patterns and agile methodolgy at high level in my project, which books/materials you will advise me to have?. Thanks – deen Apr 09 '12 at 16:05
  • @Ala: I have no idea; I do not use "agile methodology". I enjoyed Judith Bishop's book about design patterns but I have no idea if it meets your needs or not. – Eric Lippert Apr 09 '12 at 16:50
7

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.

Jean-François Fabre
  • 126,787
  • 22
  • 103
  • 165
MarcinJuraszek
  • 118,129
  • 14
  • 170
  • 241
1

Few points regarding the ToString() method in C#.

  1. ToString() method is defined in the base System.Object class and hence its available for all the types and parameters to use.

  2. The default implementation of ToString() that is provided by the system.object base class will give you the complete name of the type including the namespace.

  3. If you don't want the default implementation, then you can override the ToString() method. Yes ToString() method is overridable. And where do you override it? You override it in the class where you don't want its default implementation.

Vivek Kumar Singh
  • 2,776
  • 1
  • 13
  • 21
1

In addition to all of the other answers, it is important to understand that parts of the .NET API that implicitly use System.String.Format(), such as the Write() and WriteLine() methods found in TextWriter, Console, StringBuilder.Append() and StringBuilder.AppendLine(), etc., and of course the string.Format() implementation itself, will use the ToString() method to format an object when no explicit formatting is specified.

This means that everything that applies to ToString() above, also applies to all these other scenarios as well.

Peter Duniho
  • 62,751
  • 5
  • 84
  • 120