What are the escape sequences supported by C#?

Escape sequences in C# are special character combinations that start with a backslash (\) and represent characters that are difficult or impossible to type directly. These sequences allow you to include special characters like newlines, tabs, quotes, and control characters in your strings.

Syntax

Following is the syntax for escape sequences in C# −

string text = "Hello\nWorld";  // 
represents a newline char tab = '\t'; // \t represents a tab character

Common Escape Sequences

Escape Sequence Description Unicode
' Single quote \u0027
" Double quote \u0022
\ Backslash \u005C
\0 Null character \u0000
\a Alert (bell) \u0007
\b Backspace \u0008
\f Form feed \u000C

Newline \u000A
\r Carriage return \u000D
\t Horizontal tab \u0009
\v Vertical tab \u000B

Using Basic Escape Sequences

Example

using System;

class Demo {
   static void Main() {
      Console.WriteLine("Warning!" + '\u0007');
      Console.WriteLine("Test \t Demo Text");
      Console.WriteLine("This is it!\nThis is on the next line!");
      Console.WriteLine("She said, "Hello World!"");
      Console.WriteLine("Path: C:\Users\Documents\file.txt");
      Console.WriteLine("Single quote: 'A'");
   }
}

The output of the above code is −

Warning!
Test 	 Demo Text
This is it!
This is on the next line!
She said, "Hello World!"
Path: C:\Users\Documents\file.txt
Single quote: 'A'

Unicode Escape Sequences

C# also supports Unicode escape sequences using \u followed by a 4-digit hexadecimal number −

Example

using System;

class UnicodeDemo {
   static void Main() {
      Console.WriteLine("Copyright symbol: \u00A9");
      Console.WriteLine("Greek letter Alpha: \u03B1");
      Console.WriteLine("Heart symbol: \u2665");
      Console.WriteLine("Smiley face: \u263A");
   }
}

The output of the above code is −

Copyright symbol: ©
Greek letter Alpha: ?
Heart symbol: ?
Smiley face: ?

Verbatim Strings

You can use verbatim strings (prefixed with @) to avoid escape sequences for file paths and multiline strings −

Example

using System;

class VerbatimDemo {
   static void Main() {
      string regularPath = "C:\Users\Documents\file.txt";
      string verbatimPath = @"C:\Users\Documents\file.txt";
      
      Console.WriteLine("Regular string: " + regularPath);
      Console.WriteLine("Verbatim string: " + verbatimPath);
      
      string multiline = @"This is line 1
This is line 2
This is line 3";
      Console.WriteLine(multiline);
   }
}

The output of the above code is −

Regular string: C:\Users\Documents\file.txt
Verbatim string: C:\Users\Documents\file.txt
This is line 1
This is line 2
This is line 3

Conclusion

Escape sequences in C# provide a way to include special characters in strings and character literals. The most commonly used ones are
for newlines, \t for tabs, " for quotes, and \ for backslashes. For file paths and complex strings, consider using verbatim strings with the @ prefix to improve readability.

Updated on: 2026-03-17T07:04:35+05:30

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements