Formatting Text

One common operation is to represent objects in a textual form. By now, the first thought that should come to mind is to use ToString. In the light of localization and the fact that many types of objects can have different textual representations, the simple method provided by System.Object does not always suffice, though:

public virtual string ToString();

In particular, the lack of one or more parameters to control the desired result makes this overload not always applicable. MSDN defines the ToString method as follows:

This method returns a human-readable string that is culture-sensitive.

The IFormattable Interface

Also, the documentation states that other ToString methods can be provided through an interface called IFormattable ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.