Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a regular string and a verbatim string?

Tags:

c#

resharper

People also ask

What is the use of verbatim string in C#?

In C#, a verbatim string is created using a special 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.

What is a verbatim string literal and why do we use it?

In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals.

Which one of the following character need to be prefixed to create a verbatim string in C#?

The @ special character serves as a verbatim identifier.

How do you initiate a string without escaping each backslash?

You can use " \\ " instead of " \ " as well as '@' sign in the beginning.


A verbatim string is one that does not need to be escaped, like a filename:

string myFileName = "C:\\myfolder\\myfile.txt";

would be

string myFileName = @"C:\myfolder\myfile.txt";

The @ symbol means to read that string literally, and don't interpret control characters otherwise.


This is covered in section 2.4.4.5 of the C# specification:

2.4.4.5 String literals

C# supports two forms of string literals: regular string literals and verbatim string literals.

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

In other words the only special character in a @"verbatim string literal" is the double-quote character. If you wish to write a verbatim string containing a double-quote you must write two double-quotes. All other characters are interpreted literally.

You can even have literal new lines in a verbatim string literal. In a regular string literal you cannot have literal new lines. Instead you must use for example "\n".

Verbatim strings literals are often useful for embedding filenames and regular expressions in the source code, because backslashes in these types of strings are common and would need to be escaped if a regular string literal were used.

There is no difference at runtime between strings created from regular string literals and strings created from a verbatim string literals - they are both of type System.String.


There is no runtime difference between a string and verbatim string. They're only different at compile time. The compiler accepts fewer escape sequences in a verbatim string so what-you-see-is-what-you-get other than a quote escape.

You can also use the verbatim character, @, to tell the compiler to treat a keyword as a name:

var @if = "if";
//okay, treated as a name
Console.WriteLine(@if);
//compiler err, if without @ is a keyword
Console.WriteLine(if);

var @a = "a";
//okay
Console.WriteLine(@a);
//also okay, @ isn't part of the name
Console.WriteLine(a);

You can have multiline string too using verbatim strings:

Console.WriteLine(@"This
    is
    a
    Test
    for stackoverflow");

without @ you got an error.

In VB14 there is a new feature called Multiline Strings, it's like verbatim strings in C#.

Multiline Strings

Pro tip: VB string literals are now exactly like C# verbatim strings.


Regular strings use special escape sequences to translate to special characters.

/*
This string contains a newline
and a tab    and an escaped backslash\
*/
Console.WriteLine("This string contains a newline\nand a tab\tand an escaped backslash\\");

Verbatim strings are interpreted as is, without translating any escape sequences:

/* 
This string displays as is. No newlines\n, tabs\t or backslash-escapes\\.
*/
Console.WriteLine(@"This string displays as is. No newlines\n, tabs\t or backslash-escapes\\.");