This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
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 -> stringPublic Overridable Function ToString () As StringA string that represents the current object.
For more information about this API, seeSupplemental API remarks for Object.ToString.
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.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?