String Literals and C#

posted on Sunday, December 07, 2008 1:19 PM

I was looking for some utility that would take a very long string and convert it to vb.net 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”;

Comments


No comments posted yet.
Post Comment
Title *
Name *
Email
Url
Comment *
Please add 8 and 6 and type the answer here:

About Me

An engineer by training and a software developer at heart. My techniques and approaches meld engineering approaches with software technology.

Core to these principles is a systematic approach to the development of software with a strong lifecycle and process management emphasis through adoption of mature technologies.

Ten years designing heavy structural steel and concrete structures and 12 years in the software development profession have embedded strong project management and business knowledge in my approaches.

Subscribe to Rss Feed


Follow me on twitter @dyardy