192

I have a trial version of ReSharper and it always suggests that I switch regular strings to verbatim strings. What is the difference?

Pang
  • 9,564
  • 146
  • 81
  • 122
Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • 4
    I've never seen this suggestion from ReSharper. Can you give an example where it suggests this? – John Saunders Jul 22 '10 at 19:00
  • 4
    I just got it. The first suggestion was to move it to a resource, but the second was to make it a verbatim string: MessageBox.Show("A platypus must be petted before work can proceed."); – B. Clay Shannon-B. Crow Raven Oct 29 '14 at 17:41
  • 1
    I still get it all the time. It's generally one of the resharper warnings you want to turn off, because it suggests making strings verbatim for no reason whatsoever. – Dean Swiatek Mar 01 '18 at 19:42
  • 1
    If you like my suggested feature for the Visual Studio IDE, please upvote here: https://developercommunity.visualstudio.com/idea/602807/indent-multi-line-verbatim-strings.html – Olivier Jacot-Descombes Aug 16 '19 at 13:37

6 Answers6

218

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 12
    I wonder why Resharper suggests this change. Do any CLR gurus know if verbatim strings are processed more efficiently since escape characters can be ignored? – Matt Peterson Jul 22 '10 at 18:25
  • 9
    I think maybe ReSharper suggests it because the verbatim string is more readable. – andyp Jul 22 '10 at 18:35
  • 40
    I believe it is a kind of coding convention (I have never seen or heard before). For strings ReSharper suggest either to move it into a resource file for localization or to make it verbatim - for verbatim strings ReSharper then no longer offers the option to localize the string. So it seems like a convention to make all strings that don't need localization verbatim. – Daniel Brückner Jul 22 '10 at 18:38
  • Would using a verbatim string provide slightly better performance since there's no need to interpret escaped characters? Or is that already handled at compile time? – Mike Bruno Nov 26 '21 at 19:12
  • Generally that type of stuff is handled at compile time, if the compiler can figure out that the value is set and then never changes. At least, that's the case for primitive types. I'm not really sure about complex ones. –  Dec 01 '21 at 21:26
71

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.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
29

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);
Corbin March
  • 25,526
  • 6
  • 73
  • 100
22

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.

Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
13

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\\.");
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

If you want to suppress the ReSharper warnings, you can use:

Localizable(false)

For things like parameters, file locations, etc., this could be a good solution.

Pang
  • 9,564
  • 146
  • 81
  • 122
AndyO
  • 19
  • 2