3.12. Formatting

As we already know from our discussion of the Object class, every class in the .NET Framework class library has the ToString() method. Implementations of this method vary from class to class, but for the value types, especially for the primitive ones, we can use this method to output their values. In the following sections we will see how to use various formatting specifiers to format the values as currency, decimal, exponential, hexadecimal, and so on.

Default formatting

By default, when we use the Console.WriteLine(), or any other method that implicitly converts the value to a String, it is the ToString() method that performs the action. For example, the following code:

 '--------------------------------------- ' .NET Formatting ...

Get A Programmer's Guide to .NET 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.