String.ToString Method

July 28, 2014

Returns this instance of the string; no actual conversion is performed.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public override string ToString()

Return Value

Type: System.String
The current string.

The following code example demonstrates the ToString method. Note that the ToString method is not called explicitly; instead, it is called implicitly because the Format method uses the Composite Formatting.


using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      String str1 = "123";
      String str2 = "abc";

      outputBlock.Text += String.Format("Original str1: {0}", str1) + "\n";
      outputBlock.Text += String.Format("Original str2: {0}", str2) + "\n";
      outputBlock.Text += String.Format("str1 same as str2?: {0}", Object.ReferenceEquals(str1, str2)) + "\n";

      str2 = str1.ToString();
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("New str2:      {0}", str2) + "\n";
      outputBlock.Text += String.Format("str1 same as str2?: {0}", Object.ReferenceEquals(str1, str2)) + "\n";
   }
}
/*
This code produces the following output:
Original str1: 123
Original str2: abc
str1 same as str2?: False

New str2:      123
str1 same as str2?: True
*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft