使用Console.WriteLine
格式字符串的C#程序: -
运行此代码系统时抛出Unhandled Exception Error: -
这是堆栈跟踪: -
System.FormatException:输入字符串不是正确的格式
at System.Text.StringBuilder.AppendFormatHelper<IFormatProvider provider,string format, Params Array args>
at System.IO.TextWriter.WriteLine<String format,Object arg0,Object arg1, object arg2>
at System.IO.TextWriter.SyncTextWriter.WriteLine<String format,Object arg0,Object arg1, object arg2>
at System.IO.Console.WriteLine<String format,Object arg0,Object arg1, object arg2>
我该如何解决呢?
class Program
{
static void Main(string[] args)
{
string value1 = "Dot";
string value2 = "Net";
string value3 = "Perls";
Console.WriteLine("{ 0},{ 1},{ 2}", value1, value2, value3);
}
}
答案 0 :(得分:3)
不允许占位符内的空间。
// Bad
Console.WriteLine("{ 0},{ 1},{ 2}", value1, value2, value3);
// Good
Console.WriteLine("{0},{1},{2}", value1, value2, value3);