Oct 302012
 October 30, 2012  Posted by at 2:45 pm Not So Stupid Questions  Add comments

[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?

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).

  12 Responses to “Stupid Question 70: Verbatim identifiers in C#, should they be used and when?”

  1. 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” })

  2. 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.

  3. 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.

  4. 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”}

  5. 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;

  6. 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. 🙂

  7. 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" })

  8. […] Stupid Question 70: Verbatim identifiers in C#, should they be used and when? (Iris Classon) […]

  9. 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.

    • 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..

      • 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;}

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



What is 14 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)