In C#, a verbatim string literal is created by prefixing a string with the @ symbol. It allows backslashes and line breaks to be included in a string without interpreting backslashes as escape sequences.
- No Escape Sequences: Backslashes (\) are treated as literal characters instead of escape sequences.
- Multi-line Strings: Strings can span multiple lines while preserving line breaks and indentation.
Example: Program to demonstrates how to use verbatim strings for file paths, double quotes, and multi-line text.
using System;
public class Geeks {
static void Main()
{ // Using a verbatim string for a file path
string str = @"C:\Users";
Console.WriteLine("File Path: " + str);
// Using double quotes inside a verbatim
string str2 = @"Hello ""Geeks"" ";
Console.WriteLine("Using Double Quotes: " + str2);
// Using a multi-line verbatim strin
string str3 = @"
This is
multi line
String";
Console.WriteLine("Multi Line: " + str3);
}
}
Output
File Path: C:\Users
Using Double Quotes: Hello "Geeks"
Multi Line:
This is
multi line
String
Explanation: The @ symbol allows backslashes to be used directly, double quotes to be represented by two consecutive double quotes (""), and line breaks to be preserved inside the string.
Syntax
string str = @" string content ";
The @ symbol tells the C# compiler to treat backslashes as literal characters instead of escape characters. However, double quotation marks must still be escaped by doubling them.
Examples of Verbatim Strings
The following examples demonstrate common situations where verbatim string literals are useful.
1. Using Verbatim Strings for File Paths
File paths often contain backslashes, which must normally be escaped in regular string literals. Verbatim strings eliminate this requirement.
using System;
class Geeks {
static void Main()
{
string path = @"C:\Users\Geeks\Documents";
Console.WriteLine("File Path: " + path);
}
}
Output:
File Path: C:\Users\Geeks\Documents
Explanation: The @ symbol preserves every backslash in the file path without requiring \\.
2. Using Double Quotes in Verbatim Strings
To include double quotation marks within a verbatim string, write two consecutive double quotes ("").
using System;
class Geeks {
static void Main()
{
string message = @"Hello " "Geeks" "";
Console.WriteLine(message);
}
}
Output:
Hello "Geeks"
Explanation: Each pair of double quotes ("") inside the verbatim string represents one double quotation mark in the resulting string.
3. Creating Multi-line Verbatim Strings
Verbatim strings allow text to span multiple lines without inserting newline escape sequences.
using System;
class Geeks {
static void Main()
{
string message = @"Welcome to GeeksforGeeks Learn
C #Practice Programming ";
Console.WriteLine(message);
}
}
Output
Welcome to GeeksforGeeks Learn
C #Practice Programming
Explanation: The string preserves the line breaks as written in the source code. Any indentation included inside the string is also preserved.
4. Using Escape Sequences in Verbatim Strings
A verbatim string displays backslash sequences literally, even when those sequences have special meanings in regular strings.
using System;
class Geeks {
static void Main()
{
string str1 = "Hello\nWorld";
string str2 = @"Hello\nWorld";
Console.WriteLine("Regular String:");
Console.WriteLine(str1);
Console.WriteLine("Verbatim String:");
Console.WriteLine(str2);
}
}
Output
Regular String: Hello World Verbatim String: Hello\nWorld
Explanation: The regular string converts \n into a newline, whereas the verbatim string preserves it as two literal characters: a backslash and the letter n.
Other Use of the @ Symbol: Verbatim Identifiers
Apart from string literals, the @ symbol can also be used to create verbatim identifiers in C#. This allows reserved keywords such as for, class, and namespace to be used as variable names.
using System;
class Geeks {
static void Main()
{
string[] @for = { "C#", "Java", "Python" };
foreach (string @class in @for)
{
Console.WriteLine(@class);
}
}
}
Output
C# Java Python
Explanation:
- @for allows the reserved keyword for to be used as a variable name.
- @class allows the reserved keyword class to be used as an identifier.
- The @ symbol is not part of the identifier's actual name.
Note: Although verbatim identifiers are valid in C#, descriptive variable names are generally preferred for code readability.
Verbatim Strings with String Interpolation
C# also supports combining verbatim strings with string interpolation using $@ or @$. This allows variables and expressions to be inserted into a multi-line string while preserving backslashes.
using System;
class Geeks {
static void Main()
{
string name = "Geeks";
string path = @"C:\Users\Geeks";
string message = $@"Hello, {name}!
File Path: {path}";
Console.WriteLine(message);
}
}
Output
Hello, Geeks! File Path: C:\Users\Geeks
Explanation:
- The $ symbol enables string interpolation using expressions inside {}.
- The @ symbol preserves literal backslashes and line breaks.
- The expressions {name} and {path} are replaced with their corresponding values.
Note: C# 11 and later also support raw string literals using three or more double quotes ("""..."""). Raw string literals are another option for representing text containing quotation marks, backslashes, or multiple lines.
Regular and Verbatim Strings
Both regular and verbatim strings represent instances of the string type. The main difference is how their literal syntax handles backslashes, quotation marks, and line breaks.
Feature | Regular String | Verbatim String |
|---|---|---|
Syntax | "Hello" | @"Hello" |
Backslashes | Must be escaped when needed | Treated literally |
Newline escape (\n) | Creates a newline | Displays \n |
Double quotes | Uses \" | Uses "" |
Multi-line text | Requires escape sequences or another string form | Supports direct line breaks |
File paths | "C:\\Users\\Geeks" | @"C:\Users\Geeks" |