The ToString method converts a value to a string representation that can be expressed in multiple ways. Its precise format depends on specific symbols or a specified order defined by specific cultures, professions, or industries. You can call the method directly. It is also called automatically by the Convert.ToString(Object) and Convert.ToString(Object, IFormatProvider) methods, and by methods that use the composite formatting feature in the .NET Framework, such as String.Format(String, Object[]), Console.WriteLine(String, Object[]), and StringBuilder.AppendFormat(String, Object[]). (For more information, see Composite Formatting.)
Composite formatting methods call the ToString method once for each format item in a format string. The parameters passed to the method depend on the specific formatting method that is called and on the content of the format item, as follows:
Note
|
|
An object's ToString implementation is called by composite formatting methods only if they are not passed an ICustomFormatter format provider, or if the Format method of the custom format provider returns null.
|
The .NET Framework includes three format providers, all of which implement the IFormatProvider interface:
-
NumberFormatInfo
supplies numeric formatting information, such as the characters to use for decimal and group separators, and the spelling and placement of currency symbols in monetary values.
-
DateTimeFormatInfo
supplies date-related and time-related formatting information, such as the position of the month, the day, and the year in a date pattern.
-
CultureInfo
contains the default formatting information in a specific culture, including the numeric format information, and date-related and time-related formatting information.
In addition, you can define your own custom format provider.
Notes to Implementers
The ToString method must support the "G" (general) format specifier. Besides the "G" specifier, the class can define the list of format specifiers that it supports. In addition, the class must be prepared to handle a format specifier that is null. For more information about formatting and formatting codes, see Formatting Types.