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
What does the @ prefix do on string literals in C#?
The @ prefix in C# creates a verbatim string literal, which means you don't need to escape special characters like backslashes, quotes, or newlines. This makes the string easier to read and write, especially for file paths, regular expressions, and multi-line text.
Syntax
Following is the syntax for verbatim string literals −
@"string content here"
To include a double quote inside a verbatim string, use two consecutive quotes −
@"He said ""Hello"" to me"
Using @ for File Paths
The @ prefix eliminates the need to escape backslashes in file paths −
using System;
class Program {
static void Main() {
// Without @ prefix - requires escaping
string path1 = "D:\Users\Documents\file.txt";
// With @ prefix - no escaping needed
string path2 = @"D:\Users\Documents\file.txt";
Console.WriteLine("Escaped path: " + path1);
Console.WriteLine("Verbatim path: " + path2);
Console.WriteLine("Paths are equal: " + (path1 == path2));
}
}
The output of the above code is −
Escaped path: D:\Users\Documents\file.txt Verbatim path: D:\Users\Documents\file.txt Paths are equal: True
Using @ for Multi-line Strings
Verbatim strings preserve line breaks and formatting exactly as written −
using System;
class Program {
static void Main() {
string message = @"Welcome User,
Kindly wait for the image to
load";
Console.WriteLine(message);
Console.WriteLine();
string sql = @"SELECT Name, Age
FROM Users
WHERE Age > 18
ORDER BY Name";
Console.WriteLine("SQL Query:");
Console.WriteLine(sql);
}
}
The output of the above code is −
Welcome User, Kindly wait for the image to load SQL Query: SELECT Name, Age FROM Users WHERE Age > 18 ORDER BY Name
Using @ with Quotes Inside Strings
To include double quotes in verbatim strings, use two consecutive quote characters −
using System;
class Program {
static void Main() {
string quote = @"She said ""Hello World"" and smiled.";
string json = @"{""name"": ""John"", ""age"": 30}";
Console.WriteLine(quote);
Console.WriteLine(json);
}
}
The output of the above code is −
She said "Hello World" and smiled.
{"name": "John", "age": 30}
Comparison
| Regular String | Verbatim String (@) |
|---|---|
| "C:\Windows\System32" | @"C:\Windows\System32" |
| "Line 1\nLine 2" | @"Line 1 Line 2" |
| "He said "Hello"" | @"He said ""Hello""" |
Common Use Cases
-
File paths: Windows file paths with backslashes
-
Regular expressions: Patterns with many special characters
-
SQL queries: Multi-line database queries
-
JSON/XML: Strings containing quotes and formatting
Conclusion
The @ prefix in C# creates verbatim string literals that preserve formatting and eliminate escape sequence processing. This makes code more readable when working with file paths, multi-line text, and strings containing special characters like quotes or backslashes.
