Returns a string that represents the current object.
Assembly: mscorlib (in mscorlib.dll)
Public Overridable Function ToString As [%$TOPIC/7bxwbwt2_en-us_VS_110_1_0_0_0_0%]
public virtual [%$TOPIC/7bxwbwt2_en-us_VS_110_1_0_1_0_0%] ToString()
public:
virtual [%$TOPIC/7bxwbwt2_en-us_VS_110_1_0_2_0_0%]^ ToString()
abstract ToString : unit -> [%$TOPIC/7bxwbwt2_en-us_VS_110_1_0_3_0_0%]
override ToString : unit -> [%$TOPIC/7bxwbwt2_en-us_VS_110_1_0_3_0_1%]
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 Main()
Dim obj As New Object()
Console.WriteLine(obj.ToString())
End Sub
End Module
' The example displays the following output:
' System.Object
using System;
public class Example
{
public static void Main()
{
Object obj = new Object();
Console.WriteLine(obj.ToString());
}
}
// The example displays the following output:
// System.Object
using namespace System;
void main()
{
Object^ obj = gcnew Object();
Console::WriteLine(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.
Imports Examples
Namespace Examples
Public Class Object1
End Class
End Namespace
Module Example
Public Sub Main()
Dim obj1 As New Object1()
Console.WriteLine(obj1.ToString())
End Sub
End Module
' The example displays the following output:
' Examples.Object1
using System;
using Examples;
namespace Examples
{
public class Object1
{
}
}
public class Example
{
public static void Main()
{
object obj1 = new Object1();
Console.WriteLine(obj1.ToString());
}
}
// The example displays the following output:
// Examples.Object1
using namespace System;
namespace Examples
{
ref class Object1
{
};
}
void main()
{
Object^ obj1 = gcnew Examples::Object1();
Console::WriteLine(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 Main()
Dim obj2 As New Object2("a"c)
Console.WriteLine(obj2.ToString())
End Sub
End Module
' The example displays the following output:
' 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 Main()
{
Object2 obj2 = new Object2('a');
Console.WriteLine(obj2.ToString());
}
}
// The example displays the following output:
// Object2: a
using namespace System;
ref class Object2
{
private:
Object^ value;
public:
Object2(Object^ value)
{
this->value = value;
}
virtual String^ ToString() override
{
return Object::ToString() + ": " + value->ToString();
}
};
void main()
{
Object2^ obj2 = gcnew Object2(L'a');
Console::WriteLine(obj2->ToString());
}
// The example displays the following output:
// Object2: a
Notes for the Windows Runtime
When you call the ToString method on a class in the Windows Runtime, it provides the default behavior for classes that don’t override ToString. This is part of the support that the .NET Framework provides for the Windows Runtime (see .NET Framework Support for Windows Store Apps and Windows Runtime). Classes in the Windows Runtime don’t inherit Object, and currently don’t implement a ToString. However, they appear to have ToString, Equals(Object), and GetHashCode methods when you use them in your C# or Visual Basic code, and the .NET Framework provides the default behavior for these methods.
Note |
|---|
Windows Runtime classes that are written in C# or Visual Basic can override the ToString method. |
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 IFormattableToString(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.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note