From ReSharper, I know that
var v = @"something";
makes v something called a verbatim string. What is this and what is a common scenario to use it?
From ReSharper, I know that
var v = @"something";
makes v something called a verbatim string. What is this and what is a common scenario to use it?
In a verbatim string, escape sequences (such as "\n"
for newline) will be ignored. This helps you type strings containing backslashes.
The string is also allowed to extend over multiple lines, for example:
var s = @"
line1
line2";
The string will appear the same way you typed it in your source code, with line breaks, so you don't have to worry about indents, newlines etc.
To use quotes inside a verbatim literal, you just double them:
@"This is a string with ""quotes""."
It means that special chars don't need to be escaped, since you informed the compiler to expect special characters, and to ignore them. A common use case might be to specify a connection string:
string sqlServer = @"SERVER01\SQL";
This is perfectly valid, as opposed to in normal use where the backslash would be considered an escape character.