Question

C# Verbatim Strings: What @ Before a String Means

csharp.netstringverbatim-string

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";
string b = @"hello";

Both values are hello, and both have length 5.

Mental Model

Think of a normal string as a note that has a code system for special marks:

  • \n means “put a new line here.”
  • \t means “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.

Take Quiz

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:

  1. The compiler reads "A\nB" as a normal string literal.
  2. It interprets \n as one newline character.
  3. normal contains A, a newline, and B, so its length is 3.
  4. The compiler reads @"A\nB" as a verbatim string literal.
  5. It does not interpret \n as an escape sequence.
  6. verbatim contains A, \, n, and B, so its length is 4.

For the original values:

string hello = "hello";
string helloAlias = @"hello";

There are no backslashes, embedded quotes, or line breaks to interpret differently. Therefore both values contain the same five characters.

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"" }";
    
Take Quiz

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 = "Ava";
string folder = $@"C:\Users\{userName}\Documents";

Use $ when values must be inserted and @ when backslashes or multiline formatting should remain readable.

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

It does not. C# strings are immutable regardless of literal syntax.

string value = @"hello";
// value[0] = 'H'; // Not allowed: strings cannot be changed in place.

Using verbatim strings for every string

@ is not required for ordinary text. Use it when it improves readability; otherwise, a normal string is simple and clear.

Confusing C# verbatim strings with VB.NET syntax

This page describes C#. VB.NET has different string-literal rules, so do not assume that C# @"..." syntax works in VB.NET.

Comparisons

FeatureNormal string: "..."Verbatim string: @"..."
BackslashStarts an escape sequenceIs a normal character
Newline escape\n becomes a newline\n remains backslash + n
Literal quoteWrite \"Write ""
Multiple source linesNot directly allowed in the literalAllowed
Typical useGeneral text and escape sequencesPaths, regex, multiline text

Verbatim strings and raw string literals

Modern C# also has raw string literals, which use three or more quotes:

string json = """
{
  "name": "Ava"
}
""";

Raw string literals are often convenient for larger multiline text containing quotes. Verbatim strings remain useful, especially for concise paths and regular expressions.

@ on identifiers versus @ on strings

C# also allows @ before an identifier to use a keyword as a variable name:

string @class = "Math";

That is a separate feature from @"text", which creates a verbatim string literal.

Take Quiz

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 string type.
  • 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:

string path = $@"C:\Users\{userName}\Documents";

Does @ make a string mutable?

No. Every C# string is immutable, including strings created with @"...".

Take Quiz

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.

Take Quiz

Keep learning

Related questions

View more