tags:

views:

115

answers:

4

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

+7  A: 

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.

alc6379
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
@Matt: Common sense says that the processing is done by the compiler.
Brian
I think maybe ReSharper suggests it because the verbatim string is more readable.
andyp
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
+1  A: 

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
+5  A: 

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.

Mark Byers
+1  A: 

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