Partager via


String.ToString Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Overrides Function ToString As String
public override string ToString()

Return Value

Type: System.String
The current string.

Examples

The following code example demonstrates the ToString method.

 _

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim str1 As [String] = "123"
      Dim str2 As [String] = "abc"

      outputBlock.Text += String.Format("Original str1: {0}", str1) & vbCrLf
      outputBlock.Text += String.Format("Original str2: {0}", str2) & vbCrLf
      outputBlock.Text += String.Format("str1 same as str2?: {0}", [Object].ReferenceEquals(str1, str2)) & vbCrLf

      str2 = str1.ToString()
      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format("New str2:      {0}", str2) & vbCrLf
      outputBlock.Text += String.Format("str1 same as str2?: {0}", [Object].ReferenceEquals(str1, str2)) & vbCrLf
   End Sub 'Main
End Class 'Sample
'
'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
'
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
*/

Version Information

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

Platforms

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