System.Object.ToString Method

Returns a string that represents the current object.

Syntax

public virtual string ToString ()

Returns

A string that represents the current object.

Remarks

object.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 object.ToString method returns the fully qualified name of the type of the object, as the following example shows.

code reference: System.Object.ToString#1

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 object.ToString method. The following example illustrates this. It defines a class named Object1 that accepts the default implementation of all object members. Its object.ToString method returns the object's fully qualified type name.

code reference: System.Object.ToString#2

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

code reference: System.Object.ToString#3

Notes for the wrt

When you call the object.ToString method on a class in the wrt, it provides the default behavior for classes that don’t override object.ToString. This is part of the support that the .NET Framework provides for the wrt (see .NET Framework Support for Windows Store Apps and Windows Runtime). Classes in the wrt don’t inherit object, and don’t always implement a object.ToString. However, they always appear to have object.ToString, object.Equals(object), and object.GetHashCode methods when you use them in your C# or Visual Basic code, and the .NET Framework provides a default behavior for these methods.

Starting with the net_v451, the common language runtime will use tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.tostring.aspx on a wrt object before falling back to the default implementation of object.ToString.

Note:

wrt classes that are written in C# or Visual Basic can override the object.ToString method.

The wrt and the IStringable Interface

Starting with win81, the wrt includes an tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.aspx interface whose single method, tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.tostring.aspx, provides basic formatting support comparable to that provided by object.ToString. To prevent ambiguity, you should not implement tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.aspx on managed types.

When managed objects are called by native code or by code written in languages such as JavaScript or C++/CX, they appear to implement tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.aspx. The common language runtime will automatically route calls from tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.tostring.aspx to object.ToString in the event tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.aspx is not implemented on the managed object.

Note:

Because the common language runtime auto-implements tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.aspx for all managed types in win8_appstore_long apps, we recommend that you do not provide your own tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.aspx implementation. Implementing tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.aspx may result in unintended behavior when calling ToString from the wrt, C++/CX, or JavaScript.

If you do choose to implement tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.aspx in a public managed type that is exported in a wrt component, the following restrictions apply:

Note that under a variety of conditions, calls from native code to a managed type that implements tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.aspx or hides its tp://msdn.microsoft.com/library/windows/apps/windows.foundation.istringable.tostring.aspx implementation can produce unexpected behavior.

Example

The following example outputs the textual description of the value of an object of type object to the console.

C# Example

using System;

class MyClass {
   static void Main() {
      object o = new object();
      Console.WriteLine (o.ToString());
   }
}
      

The output is

System.Object

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0