共用方式為


Object.ToString Method

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

Updated: March 2011

Returns a string that represents the current object.

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

Syntax

'Declaration
Public Overridable Function ToString As String
public virtual string ToString()

Return Value

Type: System.String
A string that represents the current object.

Remarks

ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display. (For information about formatting support in the .NET Framework, see Formatting Types.)

The default implementation of the ToString method returns the fully qualified name of the type of the Object, as the following example shows.

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Dim obj As New Object()
      outputBlock.Text += obj.ToString()
   End Sub
End Module
' The example displays the following output:
'      System.Object
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Object obj = new Object();
      outputBlock.Text += obj.ToString();
   }
}
// The example displays the following output:
//      System.Object

Because Object is the base class of all reference types in the .NET Framework, this behavior is inherited by reference types that do not override the ToString method. The following example illustrates this. It defines a class named Object1 that accepts the default implementation of all Object members. Its ToString method returns the object's fully qualified type name.

Namespace Examples
   Public Class Object1
   End Class
End Namespace

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Dim obj1 As New Examples.Object1()
      outputBlock.Text += obj1.ToString()
   End Sub
End Module
' The example displays the following output:
'   SilverlightApplication.Examples.Object1
using System;
using Examples;

namespace Examples
{
   public class Object1
   {
   }
}

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      object obj1 = new Object1();
      outputBlock.Text += obj1.ToString();
   }
}
// The example displays the following output:
//   Examples.Object1

Types commonly override the ToString method to return a string that represents the object instance. For example, the base types such as Char, Int32, and String provide ToString implementations that return the string form of the value that the object represents. The following example defines a class, Object2, that overrides the ToString method to return the type name along with its value.

Public Class Object2
   Private value As Object

   Public Sub New(value As Object)
      Me.value = value
   End Sub

   Public Overrides Function ToString() As String
      Return MyBase.ToString + ": " + value.ToString()
   End Function
End Class

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Dim obj2 As New Object2("a"c)
      outputBlock.Text += obj2.ToString()
   End Sub
End Module
' The example displays the following output:
'       SilverlightApplication.Object2: a
using System;

public class Object2
{
   private object value;

   public Object2(object value)
   {
      this.value = value;
   }

   public override string ToString()
   {
      return base.ToString() + ": " + value.ToString();
   }
}

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Object2 obj2 = new Object2('a');
      outputBlock.Text += obj2.ToString();
   }
}
// The example displays the following output:
//       Object2: a

Notes to Implementers

When you implement your own types, you should override the ToString method to return values that are meaningful for those types. Derived classes that require more control over formatting than ToString provides can implement the IFormattable interface. Its IFormattable.ToString(String, IFormatProvider) method enables you to define format strings that control formatting and to use an IFormatProvider object that can provide for culture-specific formatting.

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.

See Also

Reference

Other Resources

Change History

Date

History

Reason

March 2011

Revised extensively.

Information enhancement.