Verbatim String allows you to ignore escape sequences and makes writing strings more natural and readable. When using escape sequences in string literals, you sometimes make a mistake of typing \\ for the “backslash” and type \ instead. This will produce an error because the compiler will think that you are starting an escape sequence and it will read the next character after the \ and process it. If it doesn’t find a matching escape sequence, then it will issue an error.
Consider the following example:
System.Console.WriteLine("I want to have a cat\dog as a birthday present."); //Error
Although it would be proper to use the / instead as in “cat/dog” in the example above, I intentionally used backslash for demonstration. The compiler will issue an error and tell you that it did not recognize the escape sequence \d clearly because no such escape sequence exists. It will be worse if the next character following the backslash is something that the compiler can recognize such as \n as seen in the following example:
System.Console.WriteLine("Answer with yes\no:");Answer with yes o
Using String Verbatim to Ignore Escape Sequences
One of the uses of string verbatim is when you don’t want your backslashes to trigger an escape sequence. The syntax for string verbatim is simple. Just prefix the string literal with the @ symbol.
System.Console.WriteLine(@"I want to have a cat\dog as a birthday present.");
I want to have a cat\dog as a birthday present.
String verbatim is commonly used when you are trying to output directories as string. Because directories contain a lot of backslashes, it would be justifiable to use string verbatim rather than using a double backslash.
If you want to print double quotations, simply use two double quotations.
System.Console.WriteLine(@"Printing ""double quotations""...");
Printing "double quotations"...
Avoid using string verbatim and escape sequences at the same time as it will also print the escape sequences in the output.
Using String Verbatim to Preserve Formatting of Strings
You can also use string verbatim to print multiline strings without the user of \n escape sequence. For example, if we are to print the following message:
C# is a great programming language and it allows you to create different kinds of applications.
Then we have to write it in C# as follows:
System.Console.WriteLine("C# is a great programming language and\n" +
"it allows you to create different\n" +
"kinds of applications.");
Notice the use of \n at the end of each line. This is to separate them in different lines as shown in the output sample of the message. The string was also divided into three just so we can save space. The + operator is used to combine the strings so that the three string literals will be combined into one. With string verbatim, we can make our lives easier when printing multiple lines of text.
System.Console.WriteLine(@"C# is a great programming language and it allows you to create different kinds of applications.");
Notice that with string verbatim, we can use one continuous string and it will be printed exactly the same as how it was typed in the editor. That’s why the second and third lines have no indentation because if we indent them, they will also be indented in the output.
Be careful when using string verbatim. Although it is useful in many circumstances, you should stick to the usual way of printing strings with escape sequences. Only use string verbatim when necessary.