Escape Sequence in C#

In C#, an escape sequence is a combination of characters that represents a special character or symbol and is used to insert such characters into a string or character literal. Escape sequences start with a backslash (\) followed by a specific character or code.

Here are some commonly used escape sequences in C#:

  1. \": Inserts a double quote character (").
  2. \': Inserts a single quote character (').
  3. \\: Inserts a backslash character (\).
  4. \n: Inserts a new line character.
  5. \r: Inserts a carriage return character.
  6. \t: Inserts a tab character.
  7. \b: Inserts a backspace character.
  8. \f: Inserts a form feed character.
  9. \uXXXX: Inserts a Unicode character represented by the hexadecimal value XXXX (e.g., \u0041 represents the letter ‘A’).
  10. \xXX: Inserts an ASCII character represented by the hexadecimal value XX (e.g., \x41 represents the letter ‘A’).
  11. \0: Inserts a null character.

Here’s an example that demonstrates the usage of escape sequences in C#:

string message = "Hello\tWorld!\nThis is a \"quoted\" string.";
Console.WriteLine(message);

Output:

Hello   World!
This is a "quoted" string.

In the above example, the escape sequence \t inserts a tab character, \n inserts a new line, and \" inserts a double quote character within the string.