Escape Sequences in C#
Escape sequences are special characters that allow you to include non-printable characters within strings. They come in handy when you need to represent characters that cannot be typed directly into a string, such as tabs (insert one-character blank space), newline, or quotes(“ “). In C#, escape sequences are denoted by a backslash (\) followed by a specific character that holds special meaning for the compiler.
Escape Sequence | Description | Example | Output |
\” | Output a double quote | “\”Hello\”” | “Hello” |
\\ | Output a backslash. Usually used for defining a path on the server e.g., C:\Windows | “C:\\Windows” | C:\Windows |
\n | Insert a new line | “Line1\nLine2” | Line1 Line2 |
\r | Insert a carriage-return, delete previous word | “Hello\rWorld” | World |
\t | Insert a tab | “Hello\tWorld” | Hello World |
\0 | Add null character between hello and world | “Hello\0World” | Hello World |
\b | Insert a backspace i.e. delete 1 character before \b | “Hello\bWorld” | HellWorld |
Verbatim strings are used to avoid using escape sequences. i.e. in case you want to write a character that is an escape sequence then we use verbatim strings to let the program know to avoid the escape sequence command created by prefixing a string literal with the @ symbol.
Verbatim String | Description | Example | Output |
@”…” | Verbatim string with newline | @”Hello\nWorld” | Hello\nWorld |
Course Video
Course Video In Englsih
Task
An escape sequence in C# is a combination of a backslash (\) followed by a character that has a special meaning, used to represent special characters in strings.
- \n: New line
- \t: Horizontal tab
- \\: Backslash
- \”: Double quote
- \’: Single quote
\r is a carriage return, often used to move the cursor to the beginning of the line without advancing to the next line.
Escape sequences like \n for new lines or \t for tabs help in formatting strings for better readability and structured output.
- \” is used to include double quotes inside a string.
- \’ is used to include single quotes inside a string.
Escape sequences allow developers to include and manage special characters in strings, making it easier to create formatted text, paths, and user-readable content.
Yes, our tutorials include content in Hindi to help native speakers understand escape sequences in C# with ease.
Our tutorials include hands-on coding examples and exercises to help you practice escape sequences in real-world scenarios.
Yes, our platform includes beginner-friendly tutorials in Hindi to make learning C# more accessible.
Learn how to correctly use escape sequences in file paths or use verbatim strings for easier handling by watching our detailed video tutorials.
Learn how to correctly use escape sequences in file paths or use verbatim strings for easier handling by watching our detailed video tutorials.