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

Maybe resolved itself, after resetting emulator and couple restarts - will let you know
@MillerMark A few weeks ago you tweeted that something huge/incredible was coming....what is/was it? (I am hoping s……
Oddly, it won't hit breakpoints in XF .net standard 2 project now either
@JamesMontemagno Updated to 15.7 preview 2 Visual Studio - now having lots of odd thread issues when starting debug……