#477 – Full List of Escape Sequences for Character Literals

You can use one of several escape sequences within a character literal to represent a character that you can’t include directly in the literal.

The full list of character literals includes:

  • \’ – Single quote (U+0027)
  • \” – Double quote (U+0022)
  • \\ – Backslash (U+005C)
  • \a – Alert (U+0007)
  • \b – Backspace (U+0008)
  • \f – Form feed (U+000c)
  • \n – New line (U+000A)
  • \r – Carriage return (U+000D)
  • \t – Horizontal tab (U+0009)
  • \v – Vertical tab (U+000B)
            char single = '\'';
            char dquote = '\"';
            char backslash = '\\';
            char alert = '\a';
            char backspace = '\b';
            char formFeed = '\f';
            char newLine = '\n';
            char cr = '\r';
            char horizTab = '\t';
            char vertTab = '\v';

You can also use a backspace followed by 0 to indicate a null character.

            char nullChar = '\0';
Advertisement

#93 – Escape Sequences in Character Literals

Just as we can include escape sequences in string literals, we can also use escape sequences in character literals to indicate special or non-printable characters.

The list of allowed escape sequences for character literals in C# is the same as for string literals, with the exception of the 8-byte Unicode literal for surrogate pairs:

  • \a  –  Bell (alert)
  • \b  –  Backspace
  • \f  –  Formfeed
  • \n  –  New line
  • \r  –  Carriage return
  • \t  –  Horizontal tab
  • \v  –  Vertical tab
  • \’  –  Single quote
  • \”  –  Double quote
  • \\  –  Backslash
  • (backslash followed by 0) – Null
  • \xhh  –  ASCII character in hex
  • \xhhhh  –  Unicode character in hex
  • \uhhhh – Unicode character  (4-byte)

Here are some examples in code:

 char c4 = '\n';    // Newline
 char c5 = '\r';    // Carriage return
 char c6 = '\t';    // Tab
 char c8 = '\'';    // Single quote
 char c9 = '\"';    // Double quote
 char c10 = '\\';   // Backslash
 char c11 = '\0';   // Null
 char c12 = '\x2E';  // hex
 char c13 = '\xe213';  // hex