Escape Sequences and Verbatim Strings in C#


Lots of escape sequences in c#

- April 20, 2015

Rest of the Story:

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";