C# Escape Sequence

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 SequenceDescriptionExampleOutput
\”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
\nInsert a new line“Line1\nLine2”

Line1

Line2

\rInsert a carriage-return,  delete  previous word“Hello\rWorld”World
\tInsert a tab“Hello\tWorld”Hello  World
\0Add null character between hello and world“Hello\0World”Hello World
\bInsert 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

1. Write a C# program that prints the following text using escape sequences
Hello, it’s a beautiful day!
“Programming is fun,” she said.
 
2. Write a C# program that prints the file path C:\Program Files\MyApp using escape sequences.
 
3. Write a C# program that prints the following table using escape sequences the existing format is like this – NameAgeAlice30Bob25
Expected Output :
Name    Age
Alice   30
Bob     25
 
4. Write a C# program that demonstrates the use of the carriage return \r to replace part of a string. The initial string should be Goodbye and after applying \r, it should be Hello.
 
5. Write a C# program that uses the backspace \b escape sequence to correct the following string from Helllo World to Hello World.
 
6. Write a C# program that inserts a null character \0 between two words and prints the string. The output should demonstrate that the null character does not affect the visible output. For example, the string should be Hello\0World.
 
7. Write a C# program that prints the string Hello\nWorld exactly as it is, without interpreting the \n as a newline character.
Frequently Asked Questions

Still have a question?

Let's talk

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.

Free video tutorials on escape sequences are available on our Academy . Watch and learn at your convenience.

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.