In trying:

string var1 = Something";
Console.WriteLine("{0} ... \{", var1);

to get:

Something ...{

I get an error: Unrecognized escape sequence.
Without the backslash I get an error: Input string was not in a correct format.
Using "{0} ... \x7B" doesn't work: Input string was not in a correct format.
(\x78 properly put a "x" there, so the escape sequence for an ASCII character value in hex works, a bit anyway...)

I have spent some time now looking around, but this little tidbit just does not seem to be covered...

string var1 = "Something";
char var2 = '{';
Console.WriteLine("{0} ... {1}", var1, var2);

This worked for me.

You have the " in the wrong place. You need one at teh start and end of strings. You have one at the end of your first line so the compiler has read "; Console.WriteLine(" as your string and then failed to work out what {0} ... \{ means as it isn valid syntax.

commented: Nice observation! +6

I apologize for the typo in the original post. I forgot a quote mark, and the text box in this forum doesn't check syntax! A real drawback!

string var1 = "Something";
Console.WriteLine("{0} ... \{", var1);

Double the brace up:

Console.WriteLine("{0} ... {{", var1);

MSDN clearly states what happens in the format string:

Opening and closing braces are interpreted as starting and ending a format item. Consequently, you must use an escape sequence to display a literal opening brace or closing brace. Specify two opening braces ("{{") in the fixed text to display one opening brace ("{"), or two closing braces ("}}") to display one closing brace ("}"). Braces in a format item are interpreted sequentially in the order they are encountered. Interpreting nested braces is not supported.

commented: And the RTM award goes to..... :) +19
commented: This is what discerns a pro from a learner like me :) +6

You're right, Narue. MSDN does indeed clearly state that.

But I didn't know that the name for what I was doing was "Composite Formatting," and it is hard to get Google to recognize "{" as part of what you're looking for, so I was unable to find that page.

I did find a page listing disallowed characters, and "{" and "}" were among them. But did MS have a "For more info" link? Noooooooooooo............

Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.