Compiler Error CS1009

无法识别的转义序列

z字符串 中反斜杠 () 的后面是一个意外的字符。编译器需要一个有效的转义字符。有关详细信息,请参阅字符 转义

下面的示例生成 CS1009。

// CS1009-a.cs
class MyClass
{
   static void Main()
   {
      // The following line causes CS1009.
      string a = "\m";   
      // Try the following line instead.
      // string a = "\t";
   }
}

发生该错误的原因通常是在文件名中使用了反斜杠字符,如下面的示例所示:

string filename = "c:\myFolder\myFile.txt";

若要纠正该错误,请使用“\”或前面带有 @ 且用引号括起的字符串,如下面的示例所示:

// CS1009-b.cs
class MyClass
{
   static void Main()
   {
      // The following line causes CS1009.
      string filename = "c:\myFolder\myFile.txt";   
      // Try one of the following lines instead.
      // string filename = "c:\\myFolder\\myFile.txt";
      // string filename = @"c:\myFolder\myFile.txt";
   }
}

请参阅

string(C# 参考)