Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
List down a list of the escape characters in C#
Escape characters in C# are special character sequences that begin with a backslash (\) and represent characters that cannot be directly typed or displayed. They are commonly used in strings, regular expressions, and character literals to represent control characters, special symbols, and Unicode characters.
Common Escape Characters
The most frequently used escape characters include for new lines, \t for tabs, and " for quotes within strings.
Example
using System;
class Program {
static void Main() {
Console.WriteLine("Hello\tWorld");
Console.WriteLine("Line 1\nLine 2");
Console.WriteLine("She said, "Hello!"");
Console.WriteLine("Path: C:\Users\Name");
}
}
The output of the above code is −
Hello World Line 1 Line 2 She said, "Hello!" Path: C:\Users\Name
Complete List of C# Escape Characters
| Escape Character | Description | Unicode Value | Example Usage |
|---|---|---|---|
| \a | Alert (bell) character | \u0007 | Console.Write("\a"); |
| \b | Backspace character | \u0008 | "Hello\bWorld" |
| \f | Form feed character | \u000C | "Page1\fPage2" |
| New line character | \u000A | "Line1\nLine2" | |
| \r | Carriage return character | \u000D | "Text\rOverwrite" |
| \t | Horizontal tab character | \u0009 | "Name\tValue" |
| \v | Vertical tab character | \u000B | "Line1\vLine2" |
| ' | Single quote | \u0027 | 'It's working' |
| " | Double quote | \u0022 | "Say "Hi"" |
| \ | Backslash | \u005C | "C:\Path\File" |
| \0 | Null character | \u0000 | "Text\0End" |
Numeric Escape Sequences
C# also supports numeric escape sequences for specifying characters by their numeric codes −
| Format | Description | Example |
|---|---|---|
| \nnn | Octal representation (up to 3 digits) | \101 (represents 'A') |
| \xnn | Hexadecimal representation (2 digits) | \x41 (represents 'A') |
| \unnnn | Unicode character (4 hex digits) | \u0041 (represents 'A') |
| \Unnnnnnnn | Unicode character (8 hex digits) | \U00000041 (represents 'A') |
Example
using System;
class Program {
static void Main() {
Console.WriteLine("Octal: \101");
Console.WriteLine("Hex: \x41");
Console.WriteLine("Unicode: \u0041");
Console.WriteLine("Unicode heart: \u2665");
Console.WriteLine("Unicode smiley: \u263A");
}
}
The output of the above code is −
Octal: A Hex: A Unicode: A Unicode heart: ? Unicode smiley: ?
Verbatim Strings
You can use verbatim strings (prefixed with @) to avoid using escape characters, especially useful for file paths −
using System;
class Program {
static void Main() {
string path1 = "C:\Users\John\Documents\file.txt";
string path2 = @"C:\Users\John\Documents\file.txt";
Console.WriteLine("Escaped: " + path1);
Console.WriteLine("Verbatim: " + path2);
Console.WriteLine("Equal: " + (path1 == path2));
}
}
The output of the above code is −
Escaped: C:\Users\John\Documents\file.txt Verbatim: C:\Users\John\Documents\file.txt Equal: True
Conclusion
Escape characters in C# provide a way to include special characters and control sequences in strings and character literals. Understanding these escape sequences is essential for proper string formatting, file path handling, and text processing in C# applications.
