C# Language String Escape Sequences

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • \' — single quote (0x0027)
  • \" — double quote (0x0022)
  • \\ — backslash (0x005C)
  • \0 — null (0x0000)
  • \a — alert (0x0007)
  • \b — backspace (0x0008)
  • \f — form feed (0x000C)
  • \n — new line (0x000A)
  • \r — carriage return (0x000D)
  • \t — horizontal tab (0x0009)
  • \v — vertical tab (0x000B)
  • \u0000 - \uFFFF — Unicode character
  • \x0 - \xFFFF — Unicode character (code with variable length)
  • \U00000000 - \U0010FFFF — Unicode character (for generating surrogates)

Remarks

String escape sequences are transformed to the corresponding character at compile time. Ordinary strings that happen to contain backwards slashes are not transformed.

For example, the strings notEscaped and notEscaped2 below are not transformed to a newline character, but will stay as two different characters ('\' and 'n').

string escaped = "\n";
string notEscaped = "\\" + "n";
string notEscaped2 = "\\n";

Console.WriteLine(escaped.Length); // 1
Console.WriteLine(notEscaped.Length); // 2            
Console.WriteLine(notEscaped2.Length); // 2


Got any C# Language Question?