Monday, August 16, 2010

Verbatim String in C#

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.
string path = @"C:\Program Files\My Program"; //verbatim literal
string path2 = "C:\\Program Files\\My Program"; //regular literal

string msg = @"Hello,
                        This is a multi-line string"; //verbatim literal

string msg2 = "Hello,\nThis is multi-line string"; //regular literal

No comments:

Post a Comment