What is the usage of escape characters in C#?

In C#, escape characters are used to represent special characters or control characters within a string. Below are some commonly used escape characters and their usage:

  1. – Line break symbol
  2. – Tabulation character
  3. backslash
  4. “- Quotation marks”
  5. ‘ – quotation mark
  6. – Carriage return
  7. \b – backspace character
  8. \f – Page break
  9. – Vertical tab

To use escape characters in a string, simply insert the appropriate escape character where needed. For example, to insert a newline character in a string, you can use \n like this:

string str = "Hello\nWorld";
Console.WriteLine(str);

The output results are:

Hello
World

It is important to note that if you need to represent a backslash character (\) directly in a string, you will need to use double backslashes (\\) because a single backslash will be interpreted as the beginning of an escape character.

bannerAds