This tutorial will explain when and how to use verbatim strings in the C# programming language.
To understand why to use verbatim strings it is important to first understand escape sequences. The first reason escape sequences are used is to create actions on printers or terminals. Examples of these actions are a bell sound from the computer or form feed for a printer. The second and more common reason today is to display characters that have special meaning like the double quote (“). An escape sequence is represented by the backslash character (\) followed by one of the characters that have meaning to the C# language. A list of many of the C# escape sequences is below.
|\a||Alert (Bell Sound)|
|\f||Form Feed (For Printers)|
|\v||Vertical Quote (For Printers)|
The \a for an alert sound is a fun way to demonstrate an escape sequence that performs an action. If we use the line
Console.WriteLine("\a"); in a C# console application the computer will make a fun (or annoying) beep sound.
From this list, the escape sequences that are more likely to be used are \’, \”, \\, and \n. The below code and its output shows an example of using these escape sequences.
In the above example, the \n is used multiple times to print a new line. The \” is used to print double quotes (“) and \\ is used to print a backslash (\).
What if there is a scenario that an application needs to display one of the escape sequences like \n or two backslashes like \\ ? That is when verbatim strings need to be used. The definition for verbatim from the Cambridge Dictionary is “in a way that uses exactly the same words as were originally used“. A verbatim string will be displayed exactly the way it is typed because the C# compiler will ignore the escape sequences and just display each character in the string.
To create a verbatim string, the string literal is prefixed with an @. There can be no spaces between the @ and the string. The below code shows an example of a verbatim string.
In the above example we can see that the string is printed…you guessed it…verbatim. The compiler did not recognize the backslashes as a signal that an escape sequence was being used and the string was printed exactly as it was typed.
Verbatim strings are also commonly used for Windows file paths. This is because with verbatim strings only one backslash (\) character needs to be typed instead of two (\\). The below code shows an example of how verbatim string file paths need less typing and have a cleaner look when reading.
Escaping ” In Verbatim Strings
The final part to cover is how to use the double quote (“) character in verbatim strings. A double quote (“) is the only character that needs to be escaped in a verbatim string. This is because double quote (“) characters are used to create strings by showing the compiler where the string begins and ends. If a double quote (“) is placed in the middle of a string then the compiler will believe that is the end of the string and the rest of the string will cause errors.
To display a double quote (“) in a verbatim string you need to escape it with a double quote (“”) before it like this “”. The code below shows an example.