Verbatim String Literals


Description:

I used to wonder, if I coud somehow represent "\" as "\" instead of the escape sequence  for black slash ("\\") in string. I have precisely come across this feature today in C-sharp and I would like to share it with you all.Especially in my last articles when I used the paths I did not feel like using "\\".

Other than the regular string literals, C# supports what is called as Verbatim string literals.Verbatim string literals begin with @" and end with the matching quote. They do not have escape sequences.

So the statement:

test=@"c:\tEST\TEST.doc";

is same as:

test="c:\\tEST\\TEST.doc";

What is needed to compile?

.NET SDK

How to Compile?

csc testVerbatimStrings.cs

Source Code:

using System;
public class TestVerbatimStrings
{
public static void Main()
{
string test;
test=@"c:\tEST\TEST.doc";
Console.WriteLine(test);
string test2;
test2="c:\\tEST\\TEST.doc";
Console.WriteLine(test2);
// @"c:\tEST\TEST.doc" is same as "c:\\tEST\\TEST.doc"
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all