C# suggest change

Verbatim String


To concatenate string literals, use the @ symbol at the beginning of each string.

var combinedString = @"\t means a tab" + @" and \n means a newline";

Interpolated verbatim string

Verbatim strings can be combined with the new http://stackoverflow.com/documentation/c%23/24/c-sharp-6-0-features/49/string-interpolation features found in C#6.

Console.WriteLine($@"Testing \n 1 2 {5 - 2}
New line");


Testing \n 1 2 3

New line

Live Demo on .NET Fiddle

As expected from a verbatim string, the backslashes are ignored as escape characters. And as expected from an interpolated string, any expression inside curly braces is evaluated before being inserted into the string at that position.

Escaping double quotes

Double Quotes inside verbatim strings can be escaped by using 2 sequential double quotes "" to represent one double quote " in the resulting string.

var str = @"""I don't think so,"" he said.";


“I don’t think so,” he said.

Live Demo on .NET Fiddle

Verbatim strings instruct compiler to not use character escapes

In a normal string, the backslash character is the escape character, which instructs the compiler to look at the next character(s) to determine the actual character in the string. (Full list of character escapes)

In verbatim strings, there are no character escapes (except for "" which is turned into a "). To use a verbatim string, just prepend a @ before the starting quotes.

This verbatim string

var filename = @"c:\temp\newfile.txt"



As opposed to using an ordinary (non-verbatim) string:

var filename = "c:\temp\newfile.txt"

that will output:

c:    emp

using character escaping. (The \t is replaced with a tab character and the \n is replace with a newline.)

Live Demo on .NET Fiddle

Multi-line strings

var multiLine = @"This is a 

multi-line paragraph";


This is a multi-line paragraph

Live Demo on .NET Fiddle

Multi-line strings that contain double-quotes can also be escaped just as they were on a single line, because they are verbatim strings.

var multilineWithDoubleQuotes = @"I went to a city named

                        ""San Diego""

                      during summer vacation.";

Live Demo on .NET Fiddle

It should be noted that the spaces/tabulations at the start of lines 2 and 3 here are actually present in the value of the variable; check this question for possible solutions.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents