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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.