C# Error CS1009 – Unrecognized escape sequence

C# Error

CS1009 – Unrecognized escape sequence

Reason for the Error & Solution

Unrecognized escape sequence

An unexpected character follows a backslash (\) in a . The compiler expects one of the valid escape characters. For more information, see .

The following sample generates 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";  
   }  
}  

A common cause of this error is using the backslash character in a file name, as the following example shows.

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

To resolve this error, use "\\" or the @-quoted string literal, as the following example shows.

// 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";  
   }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...