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:
1
2
3
4
5
6
7
8
9
| char c4 = '\n'; // Newlinechar c5 = '\r'; // Carriage returnchar c6 = '\t'; // Tabchar c8 = '\''; // Single quotechar c9 = '\"'; // Double quotechar c10 = '\\'; // Backslashchar c11 = '\0'; // Nullchar c12 = '\x2E'; // hexchar c13 = '\xe213'; // hex |

