系统抛出未处理的异常

时间:2017-11-27 09:37:02

标签: c#

使用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);
   }
}

1 个答案:

答案 0 :(得分:3)

不允许占位符内的空间。

// Bad
Console.WriteLine("{ 0},{ 1},{ 2}", value1, value2, value3);
// Good
Console.WriteLine("{0},{1},{2}", value1, value2, value3);