String Literals and C#

- April 20, 2015

Rest of the Story:

I was looking for some utility that would take a very long string and convert it to or c# with line continuation(s) characters. String literals to the rescue.  C# supports two forms of string literals: regular string literals and verbatim string literals. 

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences. 

In c# regular strings can only span multiple lines with syntax similar to the following: string sql = “SELECT customer “ +
“FROM customers “ +
“WHERE custId=10”;

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence.

In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. The above sample can be replaced with the ‘literal’ designated by the @ symbol as follows:

string sql = @“SELECT
FROM customers
WHERE custId=10”;