Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Object.ToString Method

Definition

Namespace:
System
Assemblies:
mscorlib.dll, System.Runtime.dll
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.Runtime.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll
Source:
Object.cs
Source:
Object.cs
Source:
Object.cs
Source:
Object.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Returns a string that represents the current object.

public: virtual System::String ^ ToString();
public virtual string ToString();
public virtual string? ToString();
abstract member ToString : unit -> stringoverride this.ToString : unit -> string
Public Overridable Function ToString () As String

Returns

A string that represents the current object.

Remarks

For more information about this API, seeSupplemental API remarks for Object.ToString.

Notes to Inheritors

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

Overrides of theToString() method should follow these guidelines:

  • The returned string should be friendly and readable by humans.

  • The returned string should uniquely identify the value of the object instance.

  • The returned string should be as short as possible so that it is suitable for display by a debugger.

  • YourToString() override should not returnEmpty or a null string.

  • YourToString() override should not throw an exception.

  • If the string representation of an instance is culture-sensitive or can be formatted in multiple ways, implement theIFormattable interface.

  • If the returned string includes sensitive information, you should first demand an appropriate permission. If the demand succeeds, you can return the sensitive information; otherwise, you should return a string that excludes the sensitive information.

  • YourToString() override should have no observable side effects to avoid complications in debugging. For example, a call to theToString() method should not change the value of instance fields.

  • If your type implements a parsing method (orParse orTryParse method, a constructor, or some other static method that instantiates an instance of the type from a string), you should ensure that the string returned by theToString() method can be converted to an object instance.

Applies to

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?