C#
string
@ symbol
programming
C# syntax

What's the in front of a string in C?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, the @ before a string literal means “treat this as a verbatim string.” That changes how escape sequences work and makes certain strings, especially file paths, regular expressions, and multi-line text, much easier to read.

What @ Changes in a String Literal

A normal C# string interprets backslash sequences such as \n and \t. A verbatim string does not. Most characters are taken literally.

Compare these two strings:

csharp
1string normalPath = "C:\\Users\\mark\\Documents\\notes.txt";
2string verbatimPath = @"C:\Users\mark\Documents\notes.txt";
3
4Console.WriteLine(normalPath);
5Console.WriteLine(verbatimPath);

Both produce the same output, but the verbatim version is easier to write because you do not need to double every backslash.

That is the core purpose of @ for strings. It reduces escaping noise.

Common Use Cases

The most common place you will see @ strings is file-system code:

csharp
string logFile = @"C:\Logs\app\latest.log";
Console.WriteLine(logFile);

It is also useful for regular expressions, where backslashes otherwise pile up quickly:

csharp
1using System.Text.RegularExpressions;
2
3string pattern = @"^\d{4}-\d{2}-\d{2}$";
4bool matches = Regex.IsMatch("2025-09-23", pattern);
5
6Console.WriteLine(matches);

Without @, the pattern would need extra escaping and become harder to scan.

Another nice use case is multi-line text:

csharp
1string message = @"First line
2Second line
3Third line";
4
5Console.WriteLine(message);

Because the string is verbatim, the line breaks are preserved directly in the source.

How to Include Quotes Inside a Verbatim String

The main escaping rule that remains is double quotes. In a verbatim string, you include a quote by writing it twice.

csharp
string quote = @"She said, ""Hello there.""";
Console.WriteLine(quote);

That often surprises people because backslashes stop being special, but quotes still need special handling.

Verbatim Strings and Interpolation

You can combine @ with string interpolation by writing $@"..." or @$"...". That gives you both features at once: literal backslashes and embedded expressions.

csharp
1string user = "mark";
2string path = $@"C:\Users\{user}\Downloads";
3
4Console.WriteLine(path);

This is a very common pattern in real applications. You keep the readability of a verbatim path while still inserting variables.

How This Differs From Newer Raw String Literals

Modern C# also has raw string literals using triple quotes. Those are great for JSON, SQL, markup, and other text-heavy content.

csharp
1string json = """
2{
3  "name": "Ada",
4  "role": "admin"
5}
6""";
7
8Console.WriteLine(json);

That does not make @ obsolete. Verbatim strings are still concise for paths, regex patterns, and smaller multi-line literals. Raw strings are usually better when the content itself contains many quotes or formatting characters.

So if you see @"...", think “verbatim string.” If you see """...""", think “raw string literal.”

Practical Rule of Thumb

Choose a normal string when ordinary escaping is simple and short. Choose a verbatim string when backslashes or line breaks make the normal form harder to read. Choose a raw string literal when the text itself is large or quote-heavy.

That is a readability decision, not just a syntax trick. The best form is the one that makes the code easiest to understand later.

Common Pitfalls

The most common pitfall is assuming @ disables all special rules. It mainly changes backslash handling, but embedded double quotes still need to be doubled.

Another mistake is using @ on a string and then expecting \n to become a newline. In a verbatim string, \n stays as the two characters backslash and n.

A third issue is confusing @ on strings with @ on identifiers. In C#, @class is an escaped identifier, while @"text" is a verbatim string. They are related only by syntax, not by meaning.

Finally, developers sometimes keep giant verbatim strings where a raw string literal would now be much clearer. Use the most readable modern option available in your codebase.

Summary

  • '@ before a C# string literal creates a verbatim string.'
  • Verbatim strings treat backslashes literally, which is helpful for file paths and regex patterns.
  • Multi-line text can be written directly inside a verbatim string.
  • To include a double quote inside a verbatim string, write it twice.
  • Combine @ with $ when you need both literal formatting and interpolation.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions