Skip to main content

How to Resolve Error "CS1009: Unrecognized escape sequence" in C#

The Compiler Error CS1009 is a string formatting error. The message reads: "Unrecognized escape sequence".

In C#, the backslash character \ inside a standard string literal acts as an Escape Character. It tells the compiler: "The next character has a special meaning" (e.g., \n for New Line, \t for Tab, \" for Quote).

This error occurs when you type a backslash followed by a character that is not a valid C# escape code (like \c, \s, or \P). The compiler sees the backslash, expects a special code, but doesn't recognize what follows.

This guide explains how to handle backslashes in File Paths, Regular Expressions, and normal text.

Understanding Escape Sequences

When the compiler parses "Hello\World", it looks at \W. Since \W is not a special command in C#, it throws CS1009.

If you actually want the string to contain a literal backslash character, you must "escape the escape" by typing two backslashes: \\.

Scenario 1: Windows File Paths (The Common Cause)

Windows file paths use backslashes (C:\Windows\System32). If you copy-paste a path directly into a standard C# string, it almost always triggers this error because folder names often start with letters that aren't valid escape codes.

Example of error

public void LoadFile()
{
// ⛔️ Error CS1009: Unrecognized escape sequence
// The compiler sees '\U' (in Users) and '\D' (in Documents).
// Neither \U nor \D are valid escape codes.
string path = "C:\Users\Default\Documents\file.txt";
}

Add the @ symbol before the opening quote. This tells the compiler to treat the string verbatim (literally). In a verbatim string, backslashes are treated as normal characters, not escape codes.

public void LoadFile()
{
// ✅ Correct: The '@' symbol disables escape sequence processing.
string path = @"C:\Users\Default\Documents\file.txt";

System.Console.WriteLine(path);
}

Solution B: Double Backslashes (Escaping)

You can manually escape every single backslash by doubling them.

public void LoadFile()
{
// ✅ Correct: "\\" compiles into a single backslash character.
string path = "C:\\Users\\Default\\Documents\\file.txt";
}

Solution C: Raw String Literals (C# 11+)

If you are using .NET 7 / C# 11 or newer, you can use triple quotes. This allows you to paste paths or JSON exactly as they appear.

public void LoadFile()
{
// ✅ Correct: Raw strings allow backslashes without escaping
string path = """C:\Users\Default\Documents\file.txt""";
}

Scenario 2: Regular Expressions (Regex)

Regular expressions (Regex) rely heavily on backslashes to define patterns (e.g., \d for digits, \s for whitespace). However, \d and \s are Regex codes, not C# String codes. If you put them in a standard string, C# tries to interpret them first and fails.

Example of error

using System.Text.RegularExpressions;

public void Validate()
{
// ⛔️ Error CS1009: '\d' is not a valid C# escape sequence.
var regex = new Regex("\d+");
}

Solution: Verbatim Strings

Just like file paths, always use the @ symbol for Regex patterns to prevent C# from interfering with your backslashes.

public void Validate()
{
// ✅ Correct: C# treats the string literally,
// passing "\d+" directly to the Regex engine.
var regex = new Regex(@"\d+");
}

Scenario 3: Valid Escape Sequences

Sometimes you get this error because you typoed a valid escape sequence. Here is the list of valid codes you can use in a standard string:

SequenceCharacterName
\''Single Quote
\""Double Quote
\\\Backslash
\0nullNull
\a(beep)Alert
\b(backspace)Backspace
\f(form feed)Form Feed
\n(newline)New Line
\r(return)Carriage Return
\t(tab)Horizontal Tab
\v(vertical tab)Vertical Tab
\uXXXX(unicode)Unicode Escape (e.g. \u00A9)

Any character following a \ that is not in this list causes CS1009.

Conclusion

CS1009 means you have a "naked" backslash that the compiler doesn't understand.

  1. File Paths: Use @ strings (e.g., @"C:\Path").
  2. Regex: Use @ strings (e.g., @"^\d+$").
  3. Literal Backslash: If you need to print a backslash, type \\.