C# Verbatim String Literals: How to Use Them
This article provides a practical guide to verbatim string literals
in C#, explaining what they are, why they are useful, and how to write
them in your code. You will learn how the @ symbol alters
string processing to ignore escape sequences, how to create multiline
text seamlessly, how to handle quotation marks, and how to combine
verbatim syntax with string interpolation.
What Is a Verbatim String Literal?
In C#, standard string literals interpret backslashes
(\) as escape characters. For instance, \n
represents a newline and \t represents a tab.
A verbatim string literal alters this behavior by instructing the
compiler to read the string exactly as it is typed, ignoring standard
escape sequences. Verbatim strings are defined by placing the at symbol
(@) immediately before the opening quotation mark.
Syntax and Basic Usage
To declare a verbatim string literal, prefix the string with
@:
string standard = "First Line\nSecond Line";
string verbatim = @"First Line\nSecond Line";
Console.WriteLine(standard);
// Output:
// First Line
// Second Line
Console.WriteLine(verbatim);
// Output:
// First Line\nSecond LineIn the verbatim example, \n is not converted into a line
break; it is printed literally as a backslash followed by the letter
"n".
Common Use Cases
1. File and Directory Paths
Standard string paths require every backslash to be escaped with a
second backslash (\\). Verbatim strings eliminate this
need, significantly improving readability.
// Standard string (requires escaping)
string path1 = "C:\\Users\\JohnDoe\\Documents\\file.txt";
// Verbatim string (cleaner and easier to read)
string path2 = @"C:\Users\JohnDoe\Documents\file.txt";2. Multiline Strings
Verbatim string literals can span multiple lines without concatenation or explicit newline escape sequences. Line breaks, indentation, and whitespace within the quotes are preserved exactly as written.
string query = @"
SELECT EmployeeID, FirstName, LastName
FROM Employees
WHERE Department = 'Sales'
ORDER BY LastName ASC;";3. Regular Expressions
Regular expressions often use backslashes for character classes and tokens. Verbatim strings prevent the "backslash plague" where expressions become difficult to read.
// Matches a standard date format: MM/DD/YYYY
string pattern = @"\b\d{2}/\d{2}/\d{4}\b";How to Escape Double Quotes
Because the backslash is treated literally, you cannot use
\" to insert a double quotation mark inside a verbatim
string. Instead, use two consecutive double quotation marks
(""):
string quote = @"He said, ""The project is complete."";";
Console.WriteLine(quote);
// Output: He said, "The project is complete."Combining Verbatim Strings with String Interpolation
You can combine verbatim strings with string interpolation to insert
variables into raw, multiline strings. Use both the $ and
@ symbols before the opening quote:
string folder = "Reports";
string fileName = "Annual2024.pdf";
// Both $@ and @$ are valid in C# 8.0 and later
string fullPath = $@"C:\Data\{folder}\{fileName}";
Console.WriteLine(fullPath);
// Output: C:\Data\Reports\Annual2024.pdf