Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Object.ToString Method
DefinitionIn this articleReturns a string that represents the current object.
ReturnsStringA string that represents the current object. RemarksObject.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.) Default implementations of the Object.ToString method return the fully qualified name of the object's type. Important You may have reached this page by following the link from the member list of another type. That is because that type does not override Object.ToString. Instead, it inherits the functionality of the Object.ToString method. Types frequently override the Object.ToString method to provide a more suitable string representation of a particular type. Types also frequently overload the Object.ToString method to provide support for format strings or culture-sensitive formatting. In this section: The default Object.ToString() method The default Object.ToString() methodThe default implementation of the ToString method returns the fully qualified name of the type of the Object, as the following example shows.
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
Overriding the Object.ToString() methodTypes commonly override the Object.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,
The following table lists the type categories in .NET and indicates whether or not they override the Object.ToString method.
See the Notes to Inheritors section for additional information on overriding ToString. Overloading the ToString methodIn addition to overriding the parameterless
Object.ToString() method, many types overload the The following example overloads the
The following example calls the overloaded Decimal.ToString(String, IFormatProvider) method to display culture-sensitive formatting of a currency value.
For more information on format strings and culture-sensitive formatting, see Formatting Types. For the format strings supported by numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the format strings supported by date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. Extending the Object.ToString methodBecause a type inherits the default Object.ToString method, you may find
its behavior undesirable and want to change it. This is particularly true of arrays and collection classes. While you may expect the
You have several options to produce the result string that you'd like.
Notes for the Windows RuntimeWhen 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 don't always implement a ToString. However, they always 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 a default behavior for these methods. Starting with .NET Framework 4.5.1, the common language runtime uses IStringable.ToString on a Windows Runtime object before falling back to the default implementation of Object.ToString. Note Windows Runtime classes that are written in C# or Visual Basic can override the ToString method. The Windows Runtime and the IStringable InterfaceStarting with Windows 8.1, the Windows Runtime includes an IStringable interface whose single method, IStringable.ToString, provides basic formatting support comparable to that provided by Object.ToString. To prevent ambiguity, you should not implement IStringable 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 IStringable. The common language runtime automatically routes calls from IStringable.ToString to Object.ToString if IStringable is not implemented on the managed object. Warning Because the common language runtime auto-implements
IStringable for all managed types in Windows Store apps, we recommend that you do not provide your own If you do choose to implement IStringable in a public managed type that's exported in a Windows Runtime component, the following restrictions apply:
Note that under a variety of conditions, calls from native code to a managed type that implements IStringable or hides its ToString implementation can produce unexpected behavior. Notes to InheritorsWhen 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 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. Overrides of the ToString() method should follow these guidelines:
Applies toSee also
Which method is automatically called when you pass an object as an argument?A class method is similar to an instance method, but it has a class object passed as its first argument. Recall that, when an instance method is called from an instance object, that instance object is automatically passed as the first argument to the method.
What does the acronym UML stand for?The Unified Modeling Language (UML) is a general-purpose, developmental modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a system.
What are the procedures that an object performs called?The procedures that an object performs are known as METHODS. Methods are functions that perform operations on the object's data attributes. The object is a self-contained unit consisting of data attributes and methods. Thus, code and data is not separated - this is called ENCAPSULATION.
What is another name for the accessor methods?Accessor methods, also called get methods or getters, allow a way to get the value of each instance variable from outside of the class.
|