What is @ in front of a string in C#?

The @ symbol in front of a string in C# creates a verbatim string literal. This special prefix tells the compiler to treat the string exactly as written, ignoring escape sequences and preserving formatting including line breaks.

In C#, a verbatim string is created using the @ symbol as a prefix before the opening quote. The compiler identifies this as a verbatim string and processes it literally. The main advantage of the @ symbol is to tell the string constructor to ignore escape characters and preserve line breaks exactly as they appear in the source code.

Syntax

Following is the syntax for creating a verbatim string literal −

string verbatimString = @"literal string content";

For strings containing quotes, double the quote character −

string stringWithQuotes = @"He said ""Hello"" to me";

Using Verbatim Strings for Escape Sequences

Without the @ symbol, backslashes in strings are treated as escape characters. With verbatim strings, backslashes are treated literally −

Example

using System;

class Program {
   static void Main(string[] args) {
      Console.WriteLine("Regular string with escape sequence:");
      Console.WriteLine("test string<br> test string");
      
      Console.WriteLine("\nVerbatim string (literal interpretation):");
      Console.WriteLine(@"test string <br> test string");
      
      Console.WriteLine("\nFile path examples:");
      Console.WriteLine("Regular: C:\Users\Documents\file.txt");
      Console.WriteLine(@"Verbatim: C:\Users\Documents\file.txt");
   }
}

The output of the above code is −

Regular string with escape sequence:
test string
 test string

Verbatim string (literal interpretation):
test string <br> test string

File path examples:
Regular: C:\Users\Documents\file.txt
Verbatim: C:\Users\Documents\file.txt

Using Verbatim Strings for Multi-line Text

Verbatim strings preserve line breaks and whitespace exactly as written in the source code −

Example

using System;

class Program {
   static void Main(string[] args) {
      string multilineText = @"This is line one
This is line two
    This line has extra indentation
And this is the final line";
      
      Console.WriteLine(multilineText);
      
      string sqlQuery = @"SELECT Name, Age, City 
                         FROM Users 
                         WHERE Age > 18 
                         ORDER BY Name";
      
      Console.WriteLine("\nSQL Query:");
      Console.WriteLine(sqlQuery);
   }
}

The output of the above code is −

This is line one
This is line two
    This line has extra indentation
And this is the final line

SQL Query:
SELECT Name, Age, City 
                         FROM Users 
                         WHERE Age > 18 
                         ORDER BY Name

Using Verbatim Strings with Quotes

To include double quotes within a verbatim string, double the quote character −

Example

using System;

class Program {
   static void Main(string[] args) {
      string regularString = "He said "Hello" to me";
      string verbatimString = @"He said ""Hello"" to me";
      
      Console.WriteLine("Regular string: " + regularString);
      Console.WriteLine("Verbatim string: " + verbatimString);
      
      string xmlContent = @"<person name=""John"" age=""30"">
    <city>""New York""</city>
</person>";
      
      Console.WriteLine("\nXML Content:");
      Console.WriteLine(xmlContent);
   }
}

The output of the above code is −

Regular string: He said "Hello" to me
Verbatim string: He said "Hello" to me

XML Content:
<person name="John" age="30">
    <city>"New York"</city>
</person>

Common Use Cases

Use Case Regular String Verbatim String
File Paths "C:\Users\Documents" @"C:\Users\Documents"
Regular Expressions "\d{3}-\d{2}-\d{4}" @"\d{3}-\d{2}-\d{4}"
SQL Queries Multiple concatenated lines Natural multi-line format
JSON/XML Templates Escaped quotes everywhere Doubled quotes only where needed

Conclusion

The @ symbol creates verbatim string literals in C# that preserve exact formatting and treat backslashes literally. This makes verbatim strings ideal for file paths, regular expressions, multi-line text, and any scenario where you want to avoid escape sequence processing.

Updated on: 2026-03-17T07:04:36+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements