What Does Escape Sequence Mean?
In C#, an escape sequence refers to a combination of characters beginning with a back slash (\) followed by letters or digits. Escape sequences represent non-printable and special characters in character and literal strings. As such, they allow users to communicate with a display device or printer by sending non-graphical control characters to specify actions like question marks and carriage returns.
An escape sequence is used when writing sections of code, like preprocessor definitions, to specify continuation characters, so that multiple lines of code are considered as a single line by the compiler. Regular expressions that help perform sophisticated string search operations use escape sequences to locate substrings within a large string. By enabling quoted strings, escape sequences may be used to create output files containing text template tags and files.
Techopedia Explains Escape Sequence
An escape sequence contains more than one character but functions as a single character because there is no letter to textually represent the escaped character. The compiler converts the sequence of characters into a single escaped character in the compiled program. For example, the compiler converts \n in the source code into a single character (code 10), which represents the line feed in the compiled program.
The following is a list of defined C# escape sequences with corresponding representations:
- \’ Single quotation mark used for character literals
- \” Double quotation mark used for string literals
- \\ Backslash used for file path
- \? Question mark
- \a Alert
- \b Backspace
- \f Form feed
- \n New line
- \r Carriage return
- \t Horizontal tab
- \v Vertical tab
- \0 Null
- \u Unicode escape sequence for a character with a hex value
- \x Unicode hexadecimal escape sequence - similar to \u but with variable length
- \U Unicode escape sequence used for surrogate pairs