Skip to main content

How to Write a Verbatim String Literal in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

In a normal C# string, a backslash starts an escape sequence: \n is a line break. Put @ in front of the string and a backslash is just a backslash, so @"C:\Reports\2026" means exactly what it shows.

Microsoft's reference: The advantage of verbatim strings is that escape sequences aren't processed, which makes them easy to write. The only character that still needs care is the double quote, which you write twice.

Syntax​

Syntax
string <text> = @"<characters>";
PartWhat it is
@Right before the opening quote: makes the string verbatim
<characters>Taken exactly as written, backslashes included. The string can also continue over several lines
"" inside the stringTwo double quotes give one "

For the quotes, the reference says: To include a double quotation mark in an @-quoted string, double it.

The Example​

A console app called VerbatimStrings prints a Windows path written both ways, a sentence with quotes in it, and a string that spans two lines.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n VerbatimStrings
cd VerbatimStrings
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/VerbatimStrings/VerbatimStrings.csproj:
Determining projects to restore...
Restored /Users/you/source/VerbatimStrings/VerbatimStrings.csproj (in 49 ms).
Restore succeeded.

dotnet new console -n VerbatimStrings creates a folder named VerbatimStrings with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or VerbatimStrings.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the four strings:

Program.cs (the strings)
string path = @"C:\Reports\2026\notes.txt";
string escaped = "C:\\Reports\\2026\\notes.txt";
string quote = @"She said ""hello"" and left.";
string twoLines = @"First line
Second line";
  • @"C:\Reports\2026\notes.txt" is the path as you'd type it in File Explorer.
  • "C:\\Reports\\2026\\notes.txt" is the same path in a normal string, where each backslash has to be written twice.
  • @"She said ""hello"" and left." has two pairs of double quotes, each of which becomes one ".
  • twoLines starts on one line and ends on the next: the line break becomes part of the string.

Then print them, and compare the two paths:

Program.cs (the output)
Console.WriteLine(path);
Console.WriteLine(path == escaped);
Console.WriteLine(quote);
Console.WriteLine(twoLines);

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
C:\Reports\2026\notes.txt
True
She said "hello" and left.
First line
Second line

macOS-style terminal window titled Terminal — VerbatimStrings showing dotnet run printing the path C:\Reports\2026\notes.txt, True, She said "hello" and left, and First line and Second line on two lines

The path printed exactly as written, and True shows that the verbatim and the escaped versions hold the same text. The doubled quotes printed as single ones, and the line break inside twoLines printed as a real line break.

The Complete File​

macOS-style code window titled Program.cs — VerbatimStrings showing a Windows path as a verbatim string, the same path with doubled backslashes, a verbatim string with doubled double quotes, a verbatim string over two lines, and four Console.WriteLine calls

Program.cs
string path = @"C:\Reports\2026\notes.txt";
string escaped = "C:\\Reports\\2026\\notes.txt";
string quote = @"She said ""hello"" and left.";
string twoLines = @"First line
Second line";

Console.WriteLine(path);
Console.WriteLine(path == escaped);
Console.WriteLine(quote);
Console.WriteLine(twoLines);

Common Mistakes​

MistakeWhat happensDo this instead
Writing a Windows path without @"C:\Reports\2026\notes.txt" doesn't build: error CS1009: Unrecognized escape sequence, once for \R and once for \2Add @, or double every backslash
A path where the backslash happens to form a valid escape, such as "C:\new folder"It builds, but \n is a line break: it printed C: and ew folder on two linesUse @"C:\new folder"
Escaping a quote with \" inside a verbatim stringThe build fails with error CS1003: Syntax error, ',' expected and error CS1056: Unexpected character '\'Double the quote: @"She said ""hello"""

FAQ​

When should I use a verbatim string?​

For Windows paths, for regular expressions, which are full of backslashes, and for text over several lines.

Is there an easier way for text with many quotes?​

A raw string literal, which starts and ends with three double quotes: """She said "hello" and left.""" printed the quotes as they are, without doubling them.

Conclusion​

An @ before a string makes C# take every backslash literally and lets the string run over several lines; only a double quote needs writing twice. It's the clearest way to write paths and regular expressions.

Additional Resources​