Monday, March 3, 2014

C# - String Literals

C# has two kinds of string literals – the regular ones, and verbatim string literals which are of the form @”text”. Regular string literals have to start and end on the same line of source code. A backslash within a string literal is interpreted as an escape sequence as per shown below.

Escape Sequence
Result in string
\’
Single quote (This is usually used in character literals. Character literals use the same escape sequence as string literals.)
\”
Double quote
\\
Backslash
\0
Unicode character 0
(the “null” character used to terminate C-style string)
\a
Alert (Unicode Character 7)
\b
Backspace (Unicode character 8)
\t
Horizontal tab (Unicode character 9)
\n
New line (Unicode Character 10 = 0xa)
\v
Vertical quote (Unicode character 11 = 0xb)
\f
Form feed (Unicode character 12 = 0xc)
\r
Carriage return (Unicode character 13 = 0xd)


Verbatim string literals can span multiple lines (the whitespace is preserved in the string itself), and backslashes are not interpreted as escape sequences. The only pseudo-escape sequence is for double quotes- you need to include the double quotes twice.

0 comments:

Post a Comment

Freemarket.com Marketplace