Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.ToString Method

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.


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
*/


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

Show:
© 2016 Microsoft