Verbatim Character in C#

SheoNarayan
Posted by in C# category on for Beginner level | Views : 5504 red flag

Verbatim character is one of the feature in C# that prevents the translation of string or identifier, where it is treated "as-is".

To create Verbatim string, we need to prefix it with the verbatim character (@). We can place any type of character in the Verbatim string including escape characters because they are not translated. The output of the string appears as it was written in the string.

The one exception character that is translated in verbatim string is the quote (") that is translated even in the Verbatim string. Verbatim string can contain physical line feed and escape characters too as displayed in the code below.

string verbatim = @"Start Rectangle

/////////

/ /

/ /

//////'//

End Rectangle";

You can see that in the above code snippet single quote will be ignored but if we place the double quote in between the string then the string is terminated there itself.

Using verbatim character we can use the reserved keyword of C# too, however it is not suggested.

string @for = @"Start Rectangle

/////////

/ /

/ /

/////////

End Rectangle";

string myString = @for;

In the above code, for is the reserved keyword that is used for looping in C#, however if we want to use it we can prefix it with verbatim character and use it. As written earlier this is not a good practice of use reserved keyword so we should avoid it.

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)