15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

214 ❘ ChaPTer 9 strinGs And reGulAr expressiOns<br />

How the string is formatted<br />

As an example of how strings are formatted, if you execute the following statement:<br />

Console.WriteLine("The double is {0,10:E} <strong>and</strong> the int contains {1}", d, i);<br />

Console.WriteLine() just passes the entire set of parameters to the static method, String.Format().<br />

This is the same method that you would call if you wanted to format these values for use in a string to be<br />

displayed in a text box, for example. The implementation of the three-parameter overload of WriteLine()<br />

basically does this:<br />

// Likely implementation of Console.WriteLine()<br />

public void WriteLine(string format, object arg0, object arg1)<br />

{<br />

this.WriteLine(string.Format(this.FormatProvider, format,<br />

new object[]{arg0, arg1}));<br />

}<br />

The one-parameter overload of this method, which is in turn called in the preceding code sample, simply<br />

writes out the contents of the string it has been passed, without doing any further formatting on it.<br />

String.Format() now needs to construct the final string by replacing each format specifier with a<br />

suitable string representation of the corresponding object. However, as you saw earlier, for this process of<br />

building up a string you need a StringBuilder instance rather than a string instance. In this example,<br />

a StringBuilder instance is created <strong>and</strong> initialized with the first known portion of the string, the text<br />

“The double is”. Next, the StringBuilder.AppendFormat() method is called, passing in the first<br />

format specifier, {0,10:E}, as well as the associated object, double, to add the string representation of this<br />

object to the string object being constructed. This process continues with StringBuilder.Append() <strong>and</strong><br />

StringBuilder.AppendFormat() being called repeatedly until the entire formatted string has<br />

been obtained.<br />

Now comes the interesting part: StringBuilder.AppendFormat() has to figure out how to format the<br />

object. First, it probes the object to find out whether it implements an interface in the System namespace<br />

called IFormattable. You can determine this quite simply by trying to cast an object to this interface <strong>and</strong><br />

seeing whether the cast succeeds, or by using the <strong>C#</strong> is keyword. If this test fails, AppendFormat() calls<br />

the object’s ToString() method, which all objects either inherit from System.Object or override. This is<br />

exactly what happens here because none of the classes written so far has implemented this interface.<br />

That is why the overrides of Object.ToString() have been sufficient to allow the structs <strong>and</strong> classes from<br />

earlier chapters such as Vector to get displayed in Console.WriteLine() statements.<br />

However, all the predefined primitive numeric types do implement this interface, which means that for<br />

those types, <strong>and</strong> in particular for double <strong>and</strong> int in the example, the basic ToString() method inherited<br />

from System.Object will not be called. To underst<strong>and</strong> what happens instead, you need to examine the<br />

IFormattable interface.<br />

IFormattable defines just one method, which is also called ToString(). However, this method takes two<br />

parameters as opposed to the System.Object version, which doesn’t take any parameters. The following<br />

code shows the definition of IFormattable:<br />

interface IFormattable<br />

{<br />

string ToString(string format, IFormatProvider formatProvider);<br />

}<br />

The first parameter that this overload of ToString() expects is a string that specifies the requested format.<br />

In other words, it is the specifier portion of the string that appears inside the braces ({}) in the string<br />

originally passed to Console.WriteLine() or String.Format(). For example, in the example the original<br />

statement was:<br />

Console.WriteLine("The double is {0,10:E} <strong>and</strong> the int contains {1}", d, i);<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!