Iris Classon
Iris Classon - In Love with Code

Stupid Question 70: Verbatim identifiers in C#, should they be used and when?

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

verbatim identifier, when and should they be used?

I know there is this thing called verbatim identifiers, basically you add an @ in front of a reserved C# keyword and you are free to use it as a variable.

Have a look at this msdn example:

[sourcecode language="csharp”]
class @class
public static void @static(bool @bool) {
if (@bool)

Pretty messed up right? I agree. The use of verbatim identifiers is not recommended, but I learned that it can be useful in legacy code (keywords introduced later), working with other languages or with libraries where you need to use keyword-taken-variables- by learned I mean read on the internet as I haven’t used them in my code. Because it seems weird. And they are ugly. I haven’t used verbatim identifiers, but have noticed a few devs that use them, some more freely than others. Are they often used? When would be a great usage scenario?- Or are they in general best avoided?

I’m under the impression that they are best avoided,- when possible that is. But that there might be some exceptions (and I would love to know more about these exceptions).


Leave a comment (via email)
Scott Ferguson
11/8/2012 3:39:12 PM
Reply to: Robert Friberg
What say your domain model was for schools, and you wanted a business entity to represent a school class? 
public class @class{} 

It might be less clunky than changing the entity name to SchoolClass or something else equally clumsy.. 
10/30/2012 2:59:39 PM
Verbatim identifiers come really handy when u are working with razor view engine with ASP.NET MVC and try to specify HTML attributes with HTML form helpers by using anonymous types. Sample:

@Html.TextBox("foo", @Model.Foo, new { @class = "foo" })
Steve Duitsman
10/30/2012 3:15:10 PM
The only time I've used this is in ASP.NET MVC.  If you want to set a css class on an element when you are using Html.EditorFor (or something similar) you can use it to do that.

See here.

Otherwise, I've never used it. 
Roman Mueller
10/30/2012 3:31:25 PM
Well, I am taking the risk of sounding either ignorant or arrogant:

I simply didn't know they existed in C#.

Now let me explain: In my 12 years of working with C# (is it really that long? WOW) I didn't come across a situation where I had the desire of being able to use verbatim identifiers. So, it is safe to say: I had no need for them, which explains why I didn't give a damn about their existence until now.

no matter how hard I think about them, I still cannot come up with a proper use for them in my daily work writing mostly LOB applications.

once again, my 2 cent. 
10/30/2012 4:07:23 PM
The only time I've ever found use for them is when passing in an anonymous object for HTML attributes in a Razor views and I want to specify a CSS class name; i.e. {@class="awesomeSauce"} 
Brian Schroer
10/30/2012 7:09:32 PM
I like to use them in extension methods, e.g.:

public static string ToString(this string @this, string regexPattern)
     return Regex.IsMatch(@this, regexPattern) 
          ? Regex.Match(@this, regexPattern).Value 
          : null;
Tracy Sells
10/30/2012 7:57:14 PM
I think it's a bad practice - and if you have to go back through legacy code and add a @ to the variable - then you might as well use resharper to just change the variable name.  I see this in database programming as well (allowing the escaping and using of reserved words).  I just think it is confusing and hard to maintain.

My vote is NO, NO, NO.  :) 
Kristof Claes
10/31/2012 12:17:37 AM
Are more useful scenario is adding a css class to a hyperlink using the ActionLink HtmlHelper in ASP.NET MVC. Since "class" is a reserved keyword, you wouldn't normally be able to use it while instantiating a dictionary. "@" to the rescue!

Html.ActionLink("Some Text", "SomeAction", new { @class = "class-name" }) 
Simon Colmer
10/31/2012 3:23:25 AM
Reply to: Brian Schroer
I kinda like the look of that, which I wasn't expecting :-P 
Robert Friberg
10/31/2012 7:16:25 AM
Read this post the other day and then stumbled upon something I wrote a while back using a verbatim identifier.

A pragmatic approach: When naming an identifier, choose the best possible name. If it is a keyword, compare the disadvantage of prefixing it with @ to choosing the second best possible name. 
Robert Friberg
11/12/2012 8:07:32 AM
Reply to: Scott Ferguson
But Class with an uppercase C won't clash with the keyword. But it could possibly cause a problem with VB.NET, which is case insensitive.
That brings to mind using class names as property names which is common (I do this without a second thought) but surely confusing for newbies, example:

public class Enrollment
   public Student Student{get;set;}
   public Class Class{get;set;}

Last modified on 2012-10-30