Question
In C#, what is the difference between these two string declarations?
string hello = "hello";
string helloAlias = @"hello";
Both values print the same text and have the same Length. What does the @ before a string literal do, and when does it make a difference?
Short Answer
The @ prefix creates a verbatim string literal in C#. For ordinary text such as "hello", a normal string and a verbatim string produce exactly the same value. The difference appears when the text contains backslashes, quotation marks, or line breaks.
Concept
A C# string literal is text written directly in source code.
A normal string literal uses backslash escape sequences. For example, \n represents a newline and \" represents a quotation mark.
string normal = "Line one\nLine two";
A verbatim string literal starts with @. In a verbatim string, backslashes are treated as ordinary characters rather than escape markers.
string verbatim = @"Line one\nLine two";
The value of normal contains an actual newline. The value of verbatim contains the two characters \ and n.
Verbatim strings are especially useful for Windows file paths, regular expressions, and multiline text. They do not create a different string type: both declarations create a normal System.String value.
For plain text with no escapes, these are equivalent:
string a = "hello";
b = ;
Mental Model
Think of a normal string as a note that has a code system for special marks:
\nmeans “put a new line here.”\tmeans “put a tab here.”\"means “put a quote here.”
A verbatim string is like copying text exactly as written. A backslash is just a backslash, so \n stays \n rather than becoming a new line.
The @ changes how the compiler reads the source text. Once the program is running, both forms are ordinary C# strings.
Syntax and Examples
Normal string literal syntax:
string message = "Hello\nWorld";
string quote = "She said, \"Hi!\"";
string path = "C:\\Users\\Ava\\report.txt";
Verbatim string literal syntax:
string message = @"Hello
World";
string quote = @"She said, ""Hi!""";
string path = @"C:\Users\Ava\report.txt";
Notice the rules:
- In a normal string, write a literal backslash as
\\. - In a verbatim string, write a backslash as
\. - In a normal string, include a quote with
\". - In a verbatim string, include a quote by doubling it:
"". - A verbatim string may span multiple source-code lines.
Example:
string normalPath = "C:\\Temp\\logs\\app.log";
string verbatimPath = @"C:\Temp\logs\app.log";
Console.WriteLine(normalPath == verbatimPath); // True
Both variables contain the same path. The verbatim version is often easier to read because its backslashes do not need to be doubled.
Step by Step Execution
Consider this example:
string normal = "A\nB";
string verbatim = @"A\nB";
Console.WriteLine(normal.Length);
Console.WriteLine(verbatim.Length);
Step by step:
- The compiler reads
"A\nB"as a normal string literal. - It interprets
\nas one newline character. normalcontainsA, a newline, andB, so its length is3.- The compiler reads
@"A\nB"as a verbatim string literal. - It does not interpret
\nas an escape sequence. verbatimcontainsA,\,n, andB, so its length is4.
For the original values:
string hello = ;
helloAlias = ;
Real World Use Cases
-
Windows file paths:
string filePath = @"C:\Projects\Demo\data.json"; -
Regular expressions: Backslashes are common in regex patterns, so verbatim strings reduce visual clutter.
string pattern = @"^\d{4}-\d{2}-\d{2}$"; -
Multiline messages or templates:
string emailBody = @"Hello,
Your order has shipped.
Thanks!";
- **SQL text in small examples or scripts:** Parameterized queries are still required for values, but verbatim strings can make the query text readable.
```csharp
string sql = @"SELECT Id, Name
FROM Products
WHERE IsActive = 1";
-
Text containing quotes:
string jsonFragment = @"{ ""name"": ""Ava"" }";
Real Codebase Usage
Developers choose the form that makes a literal easiest to read and least error-prone.
File and configuration paths
string configPath = @"C:\App\config\settings.json";
For paths built from parts, production code often prefers Path.Combine instead of manually joining separators:
using System.IO;
string configPath = Path.Combine("C:", "App", "config", "settings.json");
Regex patterns
using System.Text.RegularExpressions;
bool isValid = Regex.IsMatch(input, @"^[A-Za-z0-9_]+$");
Multiline templates
string message = @"Welcome, {0}!
Your account is ready.";
string formatted = string.Format(message, userName);
Interpolated verbatim strings
C# can combine interpolation with verbatim text. Both orders are valid in modern C#:
string userName = ;
folder = ;
Common Mistakes
Expecting \n to create a newline in a verbatim string
string text = @"First\nSecond";
This contains the visible characters \ and n; it does not contain a newline.
Use an actual line break instead:
string text = @"First
Second";
Or use a normal string:
string text = "First\nSecond";
Escaping quotes with \" inside a verbatim string
This is incorrect for the intended value:
string broken = @"She said, \"Hello\"";
In a verbatim string, \ is literal and does not escape the quote. Double the quote instead:
string correct = @"She said, ""Hello""";
Assuming makes a string mutable or special at runtime
Comparisons
| Feature | Normal string: "..." | Verbatim string: @"..." |
|---|---|---|
| Backslash | Starts an escape sequence | Is a normal character |
| Newline escape | \n becomes a newline | \n remains backslash + n |
| Literal quote | Write \" | Write "" |
| Multiple source lines | Not directly allowed in the literal | Allowed |
| Typical use | General text and escape sequences | Paths, regex, multiline text |
Verbatim strings and raw string literals
Modern C# also has , which use three or more quotes:
Cheat Sheet
// Normal string
string normal = "C:\\Temp\\file.txt";
string newline = "One\nTwo";
string quote = "\"Hello\"";
// Verbatim string
string verbatim = @"C:\Temp\file.txt";
string multiline = @"One
Two";
string quoted = @"""Hello""";
@"..."is a C# verbatim string literal.- It changes compiler parsing, not the runtime
stringtype. - Backslashes do not need doubling in verbatim strings.
- Write embedded quotes as
""in verbatim strings. - For plain text,
"hello"and@"hello"have the same value. - A verbatim string can contain source-code line breaks.
- Combine interpolation and verbatim syntax with
$@"...{value}...".
FAQ
What does @ mean before a string in C#?
It marks a verbatim string literal. Backslashes are treated as literal characters, and the string can span multiple lines.
Is @"hello" different from "hello"?
Not for this text. Both produce the same string value because hello has no escape sequences, quotes, or line breaks.
Why do verbatim strings use double quotes inside the text?
A quote normally ends the string. In a verbatim literal, two quotes ("") mean one quote character belongs in the value.
Can I use \n in a verbatim string?
Yes, but it remains the two characters \ and n. To include a newline, press Enter inside the verbatim literal or use a normal string with \n.
Are verbatim strings better for Windows paths?
They are often more readable because @"C:\Temp\file.txt" does not require doubled backslashes. For combining path segments, prefer Path.Combine.
Can a verbatim string use interpolation?
Yes. For example:
Mini Project
Description
Create a small configuration-text generator that stores a Windows path, a regular expression, and a multiline welcome message. This demonstrates where verbatim strings make source code easier to read.
Goal
Use verbatim strings correctly for paths, regex patterns, multiline text, and embedded quotation marks.
Requirements
Use a verbatim string for a Windows-style file path.
Use a verbatim string regular expression to validate a date in YYYY-MM-DD format.
Create a multiline welcome message with a verbatim string.
Include quotation marks inside a verbatim string.
Print the values and test the regex with one valid date.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Access Modifiers and static: public, private, protected, and Defaults
Learn how C# public, private, protected, and default access control visibility, and how static differs from instance members.