About Me

My photo
Hello, I'm a technology geek looking to upgrade my programming skills. These are some of the things I'm learning along my journey.

Thursday, April 12, 2012

Verbatim String Literals


An identifier with an @ prefix is called a verbatim identifier.
The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages.
Example 1:

public ArrayList @params {
get { return this.paramsField; }
set { this.paramsField = value; }
}

params is a C# keyword and cannot be used as a identifier unless you prefix it with the "@" symbol.

Example 2:
The problem in C# - Strings allow escape sequences.

string a = "Here's a tab:\t";

This means you must type double back slash "\\t" if you want the back slash to be shown in the string and not used as a horizontal-tab escape sequence.
Simply type

string a = @"Here's a tab:\t";

The string will not interpret the  "\" character as an escape sequence.