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

@jeffrey_doucet @Brian_Bancroft Canadians, self define themselves as NOT American, rather than being Proud Canadians
Retweeted by @dyardy Why do so many Canadians obsess over American policy that won’t impact us while being fairly ambivalent to day-to-d……
@melrobbins I was looking for 50$ earlier today, and still can't find it...ideas :)
Trump Watches Up To 8 Hours Of TV Per Day | HuffPost #SmartNews ===that explains everything…
Retweeted by @dyardy Boston