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:
- – Line break symbol
- – Tabulation character
- backslash
- “- Quotation marks”
- ‘ – quotation mark
- – Carriage return
- \b – backspace character
- \f – Page break
- – 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.