Escape Sequences and Verbatim Strings in C#

C# uses special escape sequences within a string to signify that what follows is to be treated differently.  The special character is the backslash \.  This character says to treat whatever follows it as though it were part of the string itself. 

string msg = "Spot the dog said: \" get that dog bone\"";  // knowing this the following is good syntax:

Spot the dog said: "get that dog bone" //output

List of C# Escape Sequences

\"  Display a double quotation mark
\'   Display a single quotation mark.
\\  Display a backslash.
\0  Null (non-printing).
\a  Alarm (beep terminal alarm).
\b  Backspace (back up one character position).
\f   Form feed (advance to next page).
\n  Newline (advance to next line).
\r  Carriage return (move to left margin).
\t  Tab (advance one tab space, often eight characters).
\v  Vertical tab.

C# provides a way to avoid 'escaping' characters in strings.  You can use the verbatim string literal character @ to tell VS.NET to build the string exactly as it appears.
string msg = @"go to c:\temp"; // this would work

The verbatim string can be used to allow a single string to span more than one line i.e.

string msg = @"this is great
                        to be under
                        the sun";



comments powered by Disqus


Recent Tweets

Retweeted by @dyardy Hey C# peeps, we’re trying C# in browser with reference content. Try it here:… reply with feedback /cc @LadyNaggaga
23 Amazing Vintage Photographs Taken Inside WWII Tank Factories ~ vintage everyday…
When is AI NOT Search? Artificiality intelligence==Search Engine (there cases when this is not true)
@projectedxyz Large data vs big data? Industry has so misused buzz words to sell products, and so you end up nothi……