Open In App

C# Verbatim String Literal - @

Last Updated : 11 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, a verbatim string is created using a special symbol @. The symbol(@) is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string.

The main advantage of @ symbol is to tell the string constructor to ignore escape characters and line breaks.

Syntax:

string str = @"  string content  ";

Key Features of Verbatim String

  • No Escape Sequences: It does not support backslashes(\).
  • Useful for file paths:
  • Readable multi-line strings: Allows for strings to span multi lines.

Example:

C#
using System;

public class Geeks
{
    static public void Main ()
    {
      	// using for file path
        string str = @"C:\Users";
      	Console.WriteLine("File Path: " + str);
      
      	// using double quotes
      	string str2 = @"Hello ""Geeks"" ";
        Console.WriteLine("Using Double Quotes: " + str2);
      
      	// using multi line
      	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

Types of Verbatim String Literal

There is mainly three uses of @ symbol which is as follows:

  • Verbatim String Identifier
  • Verbatim String Literal

1. Verbatim String Identifier

This symbol allows using a keyword as an identifier. The @ symbol prefixes the keyword, so the compiler takes keyword as an identifier without any error as shown in the below example:

Example: 

C#
// C# program to illustrate
// the use of @ by using keyword
// as an identifier
using System;

public class GFG 
{
    // Main method
    static public void Main()
    {

        // Creating and initializing the array
        // here 'for' keyword is used as 
        // an identifier by using @ symbol
        string[] @for = {"C#", "PHP", "Java", "Python"};

        // as and for keywords is 
        // as an identifier
        // using @ symbol
        foreach (string @as in @for) {
           Console.WriteLine("Element of Array: {0}", @as);
        }
    }
}

Output
Element of Array: C#
Element of Array: PHP
Element of Array: Java
Element of Array: Python

2. Verbatim String Literal

For printing the escape sequences in string literals and also using the line breaks etc. in a string literal without any escape sequence.

If one will put the escape sequence like "\\" (for backslash), "\u" (Unicode escape sequence), "\x" (hexadecimal escape sequence) etc. in a string literal without using @ symbol then these sequences will be interpreted by compiler automatically. But "" (double quotes) are not interpreted literally. Its like a string interpolation.

Example:

C#
// Using Verbatim String Literal
using System;

public class Geeks
{
    static public void Main ()
    {
      	// Verbatim String Literal
        string str = @"This is \n a verbatim string";
      	Console.WriteLine(str);
    }
}

Output
This is \n a verbatim string

Use Cases with Example

Let's see different cases with and without @ symbol.

Case 1:

// taking a string literal and
// try to print double quotes
string str1 = """";

// printing output
// this will give compile
// time error as Unexpected
// symbol `'
Console.WriteLine(str1);

In the above program, the double quotes inside double quotes as a string literal are interpreted as a single quotation mark.

Case 2:

// taking a string literal prefixes
// with @ and try to print double quotes
string str1 = @"""";

// printing output
// this will output as "
Console.WriteLine(str1);

In the above program, the output is double quote(") not ""

Case 3:

// taking a string in which we are storing
// some location of file but \Testing will
// interpreted as escape sequence \T
// similarly \N
string str1 = "\\C:\Testing\New\Target";

// printing str1
// this will give compile time error as
// Unrecognized escape sequence `\T'
// Unrecognized escape sequence `\N'
// Unrecognized escape sequence `\T'
Console.WriteLine(str1);

Case 4:

// taking a string and prefix literal with @ symbol.
// Storing some location of file
string str1 = @"\\C:\Testing\New\Target";

// printing str1 will give output as
// \\C:\Testing\New\Target
Console.WriteLine(str1);

Program: 

CSharp
// C# program to illustrate
// the use of @ in terms of 
// escape sequences and new 
// line and tab
using System;

public class GFG {

    // Main method
    static public void Main()
    {

        // If you use the below commented
        // the part then this will give
        // Unrecognized escape sequence error
        // string S1 = "\\welcome \to GeeksforGeeks \ portal \";
        // Console.WriteLine("String 1 is :{0}", S1);

        // By using @ in the given string 
        // it runs smoothly because
        // @ symbol tells the compiler to
        // ignore all escape sequences
        string S2 = @"\\welcome \to GeeksforGeeks \ portal \";
        Console.WriteLine("String 2 is: {0}", S2);

        // printing new line character in string literal
        // but it will make the string to break  
        // into a new line, see output
        string S3 = "This is \n C# non verbatim string";
        Console.WriteLine("String 3 is :{0}", S3);

        // By using @ symbol /n does not processed
        string S4 = @"This is \n C# verbatim string";
        Console.WriteLine("String 4 is :{0}", S4);

        // printing a string literal contains 
        // tabs and new line without using 
        // any escape sequence
        Console.WriteLine(@"Without Tab Sequence and New Line Character
                               C          C++      Java       Python");
    }
}

Output
String 2 is: \\welcome \to GeeksforGeeks \ portal \
String 3 is :This is 
 C# non verbatim string
String 4 is :This is \n C# verbatim string
Without Tab Sequence and New Line Character
                               C          C++      Java       Python

Next Article
Article Tags :

Similar Reads