gerry said:
I don't understand the original question and I am flabergasted by the
responses.
It sounds like the op is trying to print the string "{0}" to the console
along with some other values.
However he has answered his own question :
Console.WriteLine("Hello {{0}} {0}", "John");
displays :
Hello {0} John
which is exactly what it sounds like he is trying to do.
the second line gives an error , and it should - so don't do it that way.
string.format("text {0}","value of 0") : huh ?
this generates "text value of 0" , how is that printing "{0}" ?
Console.WriteLine ( String.Format ("Hello {0}", "John") ); huh ?
this generates "Hello John" , how is that printing "{0}" ?
also : why bother with string.Format() when Console.WriteLine() has this
formatting capability already.
To add a bit to your post, there is a situation where doubling up the
closing brace on a format specifier causes a problem:
Say you want to print out the number 42:
Console.WriteLine("{0}", 42); // prints "42"
Console.WriteLine("{0:N}", 42); // prints "42.00"
No problems so far, but let's say you want to print the number 42
enclosed in braces, so you slap a couple of escaped braces around the
format specifier:
Console.WriteLine("{{{0}}}", 42); // OK, prints "{42}"
Console.WriteLine("{{{0:N}}}", 42); // Oops, prints "{N}"
the format string parser gets confused in the second situation.
Apparently this is 'by design', so to work-around it you have to jump
through a hoop or two. Something like:
Console.WriteLine("{{{0:N}{1}", 42, '}'); // OK, prints "{42.00}"
Console.WriteLine("{{{0}}}", 42.ToString( "N")); // OK, prints "{42.00}"
Brad Abrams discusses this (and I stole the above examples from) here:
http://blogs.msdn.com/brada/archive/2004/01/14/58851.aspx
Of course, all of your comments are spot-on; I just wanted to add a bit
of trivia to this brace-escaping thread, since it caused a bit of
head-scratching on my part when I ran into it.