#65 – Verbatim String Literals

Because the backslash (\) is the first character in an escape sequence, you need to use the double-backslash sequence (\\) to embed actual backslashes in a string literal.

 string file = "C:\\MyDir\\Another Dir\\thefile.txt";

Because this can get a little hard to read, C# allows using the at sign (@) character to indicate a verbatim string literal–a string literal in which escape sequences should not be interpreted.

Using a verbatim string literal, we can write the earlier string without doubling the backslashes:

 string file = @"C:\MyDir\Another Dir\thefile.txt";

We can also use a verbatim string literal to split a string across multiple lines in the source code, rather than embedding the \n escape sequence:

string file = @"First line
Second line
Third line";
Advertisements