IFormattable interface

T

Tony Johansson

Hi!

This class Price implements the IFormattable interface below.
When I have this kind of statement
Console.WriteLine("{0:N2}", p);
in main the implemented method ToString from interface IFormattable is
called.
But if I for example just replace this {0:N2} with "test" the method
ToString is not called.
So it seems to me that the compiler in some way check the first argument for
a valid
format. So if a valid format it will call the ToString.

Do I have right in my suggestions ?

class Price : IFormattable
{
double _price;
public Price(double value)
{
this._price = value;
}

public String ToString(String format, IFormatProvider fp)
{
if (format == "$")
{
return "$" + this._price.ToString("0.00");
}
else if (format == null || format.Trim().Equals(""))
{
return this._price.ToString();
}
else
{
return format + " " + this._price.ToString();
}
}

static void Main(string[] args)
{
Price p = new Price(15.5);
Console.WriteLine("{0:N2}", p);
Console.WriteLine(p);
Console.ReadLine();
}
}

//Tony
 
P

Peter Duniho

Tony said:
Hi!

This class Price implements the IFormattable interface below.
When I have this kind of statement
Console.WriteLine("{0:N2}", p);
in main the implemented method ToString from interface IFormattable is
called.
But if I for example just replace this {0:N2} with "test" the method
ToString is not called.
So it seems to me that the compiler in some way check the first argument for
a valid
format. So if a valid format it will call the ToString.

Do I have right in my suggestions ?

No. And again, you should look at the compiled IL for the details.

But the basic answer is that compiler is simply passing whatever string
is provided to the WriteLine() method. It's the formatting process done
at run-time that then, based on the string, conditionally retrieves the
remaining arguments, calling ToString() as necessary on those arguments
(e.g. the Price object referenced by your "p" variable).

Pete
 
T

Tony Johansson

Peter Duniho said:
No. And again, you should look at the compiled IL for the details.
But the basic answer is that compiler is simply passing whatever string
is provided to the WriteLine() method. It's the formatting process done
at run-time that then, based on the string, conditionally retrieves the
remaining arguments, calling ToString() as necessary on those arguments
(e.g. the Price object referenced by your "p" variable).

Pete


Silly question here but if ToString is called when having this argument
{0:N2}
why is ToString not called when having for example a simple string as test.

So in some why a parsing is done on the first argument to decide whether or
not the ToString
method should be called or not.

//Tony
 
P

Peter Duniho

Tony said:
Silly question here but if ToString is called when having this argument
{0:N2}
why is ToString not called when having for example a simple string as test.

If you have this:

Console.WriteLine("test", p);

…where in the output string would "p" be incorporated?

Of course, the answer is "nowhere". And so if "p" is not to be
incorporated in the output, why would the formatter make any attempt to
format "p" at all?
So in some why a parsing is done on the first argument to decide whether or
not the ToString
method should be called or not.

Yes, of course. That's the whole point of formatted output.

To parse the format string (passed as the first argument), and _if_ some
other input argument is specified (via the construct "{<argument
#>[:<argument format>]", which in your example looks like "{0:N2}",
indicating that the first input argument should be formatted using the
string "N2"), then and only then does the formatter attempt to format
the input argument using the given format information.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top