Verbatim Identifiers and verbatim string literals in C#

Browsing through some of my old answers on StackOverflow, I came across this question I had answered regarding verbatim identifiers in the C# language. I want to take a moment to talk about this mysterious language feature, because it actually has multiple usages, one which is quite common, and you just might not be aware you are using it.

To start, I’ll describe to you what a verbatim identifier actually is by citing the C# language specification.

The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

This text comes from section 2.4.2 of the language specification. It is also interesting to note that this language feature has been available since the first version of C#, specifically version 1.0 and 1.2, but it is not a feature that you would normally seek out as you learn a language. Good programmers generally try to find better names for variables than ‘object’ or ‘string’, especially since they conflict with existing type identifiers.

A valid usage of this language feature would be the following.

public string ToString(object @object)
    string @string = @object as string;

    if (@string != null)
        return @string;

    throw new ArgumentException("@object is not of type System.String.");

This is a terrible piece of code, but it does demonstrate the usage of the language feature correctly. But the language feature itself is a bit more flexible than that, but I don’t believe by design. We would of course have to ask the language designers, but let’s look at the code from the question I answered on StackOverflow.

_scale_id_regex = @ConfigurationSettings.AppSettings["some_setting"];

The gentlemen asked what the purpose of this symbol was in this piece of code. What I described earlier is pretty much the extent of what is outlined by the language specification, but the short answer is that it does not serve any real purpose. It is simply allowed by the language rules, and thus allowed by the compiler implementation. You can look at it a bit deeper however. Almost everything in code that has a name is considered an identifier, in this case ConfigurationSettings is the identifier, and verbatim identifiers are allowed by the language rules.

You’re hopefully wondering by now what this has to do with string literals, aha! String literals. A string literal in C# is identified by the usage of @ at the beginning of zero or more characters enclosed in double quotes, as in @"The fox jumped over the hole.". Again, we should reference the C# language specification to describe a string literal.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escaped-sequence. In particular, simple escape sequences, and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

We never really think about verbatim identifiers, and we don’t think about string literals as verbatim string literals as programmers. We just think of string literals, because that is the common terminology, but the usage of the @ symbol to specify something as verbatim dates back to C# 1.0 and 1.2 with verbatim identifiers and string literals. The language feature is still very much alive into C# 5.0 and while verbatim identifier usage is very rare (and should be), verbatim string literals are a daily routine for programmers.

Leave a Comment

Your email address will not be published.