#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

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

2 Responses to #93 – Escape Sequences in Character Literals

  1. Peter Chamberlin says:

    The character literal for null, , is not appearing on the rendered page.

  2. Gus Gustafson says:

    really poor choice of variable names; the trailing comment should be the variable name; imaging running across “c4” in the code!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: