简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM