Yinglin.Jiang
Yinglin.Jiang

Reputation: 11

C# Console WriteLine System.FormatException:

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

Answers (1)

DiskJunky
DiskJunky

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

Related Questions