String.ToString Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns this instance of the string; no actual conversion is performed.
Assembly: mscorlib (in mscorlib.dll)
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 */
Show: