Tuesday, 16 October 2012

What is a verbatim string literal (@)?

A verbatim string literal is a string literal preceded by the "at sign" (@). For example:

@"c:\Program Files"
 
The @ character indicates that no character interpretations should be applied between the subsequent quotation marks ("). So, as in the preceding example, verbatim string literals can contain unescaped backslashes—the backslash (\) is an escape character in regular string literals—and line separators such as carriage return (CR) and linefeed (LF). A normal string literal would look like this:

"c:\\Program Files"
To insert a double quote (") within a verbatim literal, simply double it like this:

@"Her child is ""difficult""."
 
Typical uses for verbatim string literals include:
  • file path specifications
  • SQL strings
  • white space inculsions (e.g. line separators)
  • XML strings

No comments :