Verbatim String in C#

 As you know that strings are initialized a value like this,
  1. string name = "shanawar":  
Sometimes certain string values can create problem. Foe example if you want to display the following message,

Hello: "Welcome to C#".

If you try to write this message as a normal string, like,
  1. string message = " Hello: "Welcome to C#" ";  
Now visual studio gets confused because the double quotes are used to delimit the start and end of the string, not as the part of the string itself.
To solve this problem C# uses special escape sequence characters . The table below is showing the escape sequences and their interpretation.



You have to write the string using escape sequences,
  1. string message = " Hello: \"Welcome to C#\" ";  
Similarly if you want to display directory path in any drive. You would need to write the following code,
  1. string path = "C:\\Program Files\\Tutorials\\C Sharp";  
This looks not good, C# provides a way to make such strings prettier. You can use Verbatim string literal character '@' to tell the visual studio to build this string exactly as it appears in the double quotation marks, Therefore the string,
  1. string path = @"C:\Program Files\Tutorials\C Sharp";  
Will display the path exactly you have written it in the double quotes without using escape sequences. Verbatim strings are not used often, but are needed when you need to pass directory information around your program.
Ebook Download
View all
Learn
View all