EN
C# / .NET - escape sequences
0
points
In this article, we would like to show you escape sequences in C#.
| Escape sequence | Description |
|---|---|
| \n | Inserts a new line. |
| \t | Inserts a tab. |
| \b | Inserts a backspace. |
| \r | Inserts a carriage return. |
| \f | Inserts a form feed. |
| \" | Inserts a double quote character. |
| \\ | Inserts a backslash. |
Practical examples
Example 1
In this example, we use \\ escape sequence to insert backslash into the path string.
using System;
public class Program
{
public static void Main()
{
string path = "C:\\projects\\example";
Console.WriteLine(path);
}
}
Output:
C:\projects\example
Example 2
In this example, we use \n escape sequence to insert a new line between the words.
using System;
public class Program
{
public static void Main()
{
string text = "line1\nline2\nline3";
Console.WriteLine(text);
}
}
Output:
line1
line2
line3
Example 3
In this example, we use \" escape sequence to insert a double quote character.
using System;
public class Program
{
public static void Main()
{
string quote = "\"double-quoted text\"";
Console.WriteLine(quote);
}
}
Output:
"double-quoted text"