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

  1. using System;
  2. public class TestVerbatimStrings
  3. {
  4. public static void Main()
  5. {
  6. string test;
  7. test = @"c:\tEST\TEST.doc";
  8. Console.WriteLine(test);
  9. string test2;
  10. test2 = "c:\\tEST\\TEST.doc";
  11. Console.WriteLine(test2);
  12. // @"c:\tEST\TEST.doc" is same as "c:\\tEST\\TEST.doc"
  13. }
  14. }