This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its string representation and to embed that representation in a string. The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.
The provider parameter supplies custom and culture-specific information used to moderate the formatting process. The provider parameter is an IFormatProvider implementation whose GetFormat method is called by the String..::.Format(IFormatProvider, String, array<Object>[]()[]) method. The method must return an object to supply formatting information that is of the same type as the formatType parameter. The provider parameter's GetFormat method is called one or more times, depending on the specific type of objects in args, as follows:
It is always passed a Type object that represents the ICustomFormatter type.
It is passed a Type object that represents the DateTimeFormatInfo type for each format item whose corresponding data type is a date and time value.
It is passed a Type object that represents the NumberFormatInfo type for each format item whose corresponding data type is numeric.
For more information, see the Format Providers section of the Formatting Overview topic. The Example section provides an example of a custom format provider that outputs numeric values as customer account numbers with embedded hyphens.
The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. The formatting process replaces each format item with the string representation of the value of the corresponding object.
The syntax of a format item is as follows:
{index[,length][:formatString]}
Elements in square brackets are optional. The following table describes each element.
Element | Description |
|---|
index
| The zero-based position in the parameter list of the object to be formatted. If there is no parameter in the index position, a FormatException is thrown. If the object specified by index is nullNothingnullptra null reference (Nothing in Visual Basic), the format item is replaced by String..::.Empty. |
,length | The minimum number of characters in the string representation of the object to be formatted. If positive, the object to be formatted is right-aligned; if negative, it is left-aligned. The comma is required if length is specified. |
:formatString | A standard or custom format string that is supported by the object to be formatted. If formatString is not specified and the object to be formatted implements the IFormattable interface, nullNothingnullptra null reference (Nothing in Visual Basic) is passed as the value of the format parameter used as the IFormattable..::.ToString format string. |
The leading and trailing brace characters, '{' and '}', are required. To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".
If the value of format is, "Thank you for your purchase of {0:####} copies of Microsoft®.NET (Core Reference).", and arg[0] is an Int16 with the value 123, then the return value will be:
"Thank you for your purchase of 123 copies of Microsoft®.NET (Core Reference)."
If the value of format is, "Brad's dog has {0,-8:G} fleas.", arg[0]is an Int16 with the value 42, (and in this example, underscores represent padding spaces) then the return value will be:
"Brad's dog has 42______ fleas."