logo search
CSharp_Prog_Guide

Метод ToString и описатели формата

Платформа .NET Framework поддерживает перегрузку метода ToString, существующего у любого типа и выполняющего элементарное форматирование, а также специализированную версию метода ToString, использующую параметр описателя формата для указания, как нужно форматировать значение.

Format Providers

Format providers supply information such as the character to use as the decimal point when formatting numeric strings, or the separation character to use when formatting a DateTime object. Format providers define the characters used for formatting by the format specifiers, but do not define the specifiers themselves.

A format provider can either be passed to the overload of ToString required by the IFormattable interface, or be predetermined by the method you are using to format text if no format provider is passed.

When no format provider is passed, the information is either inferred, or obtained from one of the standard format providers included in the .NET Framework. Generally, classes that implement IFormattable also provide overloads of ToString that accept only a format specifier or only a format provider. The default ToString method, which accepts no parameters, is inherited from the Object class.

Composite Formatting

The .NET Framework composite formatting feature, which is supported by methods such as String..::.Format and the output methods of System..::.Console and System.IO..::.TextWriter, replaces each indexed format item embedded in a source string with the formatted equivalent of a corresponding element in a list of values.