51

Why does the string data type have a .ToString() method?

Yuck
  • 49,664
  • 13
  • 105
  • 135
CJ7
  • 22,579
  • 65
  • 193
  • 321

6 Answers6

76

The type System.String, like almost all types in .NET, derives from System.Object. Object has a ToString() method and so String inherits this method. It is a virtual method and String overrides it to return a reference to itself rather than using the default implementation which is to return the name of the type.

From Reflector, this is the implementation of ToString in Object:

public virtual string ToString()
{
    return this.GetType().ToString();
}

And this is the override in String:

public override string ToString()
{
    return this;
}
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
10

As Mark points out, it's just returning a reference to itself. But, why is this important? All basic types should return a string representation of themselves. Imagine a logging function that worked like this:

public void Log(object o) {
    Console.WriteLine(o.ToString());
}

This allows you to pass any basic type and log it's contents. Without string returning itself, it would simply print out "String" rather than it's contents. You could also do the same thing with a template function.

Think this is silly? That's basically what the string formatting functions do. It calls "ToString" when you do this:

Console.WriteLine("{0}", myString);
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
3

String is an object, it is not a data type. Because String is an object, it inherits from the Root Object the ToString() method.

It just like in Java, Objective-C or Scala:)

vodkhang
  • 18,639
  • 11
  • 76
  • 110
  • it suprised me:), I just thought that C# is like Java, there is no toString method for those things:) – vodkhang May 06 '10 at 07:02
  • it is not capitalized because it is an alias. After compiling, that alias will be converted to the capitalized String – vodkhang May 06 '10 at 07:03
  • no, string is never boxed to become an object. http://stackoverflow.com/questions/215255/string-vs-string-in-c – vodkhang May 06 '10 at 07:04
3

This is even true for java , I think most of the Object Oriented programing languages have this , a string representation of objects in question, since every class you create by default it extedns from Object thus resulting in having the toString() method , remember it's only applicable to objects not for premitive types.

G.E.B
  • 76
  • 1
  • 3
1

You'll get a Null Reference Exception if your string is NULL and you use .ToString();

The following will throw:

string.Format("msgBoxTitle = {0}", msgBoxTitle.ToString())

Best to just write... This won't throw.

string.Format("msgBoxTitle = {0}", msgBoxTitle)
Mark Gerrior
  • 378
  • 2
  • 11
0

Any object in C# has a to string method, although i can't think of a reason why one would cast a string to a string at the moment the ToString() is inherited from the object type, which of course a string is an example of.

Stephen Murby
  • 1,407
  • 3
  • 17
  • 37