Compiler Error CS1009
Unrecognized escape sequence
An unexpected character follows a backslash (\) in a string of an escape sequence or character literal. The compiler expects one of the valid escape characters. For more information, see Character Escapes.
The following sample generates CS1009.
// CS1009-a.cs
class MyClass
{
static void Main()
{
// The following escape sequence causes CS1009:
string a = "\m";
// Try the following line instead.
// string a = "\t";
// The following character literals causes CS1009:
// CS1009; a lowercase \u-style Unicode escape sequence must have exactly 4 hex digits
string unicodeEscapeSequence = '\u061';
// CS1009; a hex escape sequence must start with lowercase \x
string hexEscapeSequence = '\X061';
// CS1009; an uppercase \U-style Unicode escape sequence must have exactly 8 hex digits
string uppercaseUnicodeEscape = '\U0061';
}
}
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";
}
}
See also
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for