Reputation: 11
string CPP_NAME_SPACE = "test"
System.Console.WriteLine("} // namespace {0}", CPP_NAME_SPACE); // FormatException
System.Console.WriteLine("} // namespace {0}"); // normal
System.Console.WriteLine("// namespace {0}", CPP_NAME_SPACE); // normal
Why does the first line throw a FormatException but not the second?
Upvotes: 1
Views: 359
Reputation: 4971
The first line throws an exception because it's trying to populate the {0}
with a value and there's an unescaped brace }
at the start (it should be }}
to avoid the exception). The }
at the start is treated as a formatting code which is incomplete so it cannot process it.
The second line doesn't as there's no value to populate the string with so it prints as is.
Upvotes: 8