.Net, C#, VB.NET

Keywords as variables/identifiers. Why?

I use words variable and identifier interchangeably.

Have you ever tried to use a keyword as a name of your variable?  Until recently I thought it was impossible.  However, C# and VB.NET allow developers to use keywords as variables.

string string = '' '';
Dim String As String = '' ''

If you try to compile this code, compiler will generate three errors:

  • Error    1    Identifier expected; ‘string’ is a keyword
  • Error    2    Identifier expected
  • Error    3    Invalid expression term ‘string’ 

To fix this error in C# you just prefix your variable with an @ character.  In VB.NET you surround the variable with square brackets [].  A variable or identifier with an @ prefix is called a verbatim identifier.

string @string = '' '';
Dim [String] As String = '' ''

Why anybody would use keywords as variables?  To me it looks like a very BAD practice.  Variables/identifiers must be descriptive.  Can you think of any keyword that would be descriptive enough to use in your code as a variable?

I found in C# specification why Microsoft includes verbatim identifier in their languages:

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.

I still believe it’s a BAD idea.

I also understand that if today I don’t see any reason to use verbatim identifiers, tomorrow I might find a perfect application for it.

If you use it and have a good reason, please let me know.

kick it on DotNetKicks.com


9 thoughts on “Keywords as variables/identifiers. Why?

  1. Because not all languages have the same rules.

    In ASP.NET MVC (C#) views, I need to specify an attribute for HTML via an anonymous type. HTML requires that it is called “class”. But that’s a keyword in C#. So I can do new { @class = “required” }.

    Also, if consuming an assembly written in one language using a different language, you may need to do this.

    1. Craig,

      Thanks. I like your example with ASP.NET MVC and anonymous type. I guess you can use in example like this one:

      I didn’t have to wait until tomorrow. :)

  2. The only time this shown up for me is in code generation where identifiers are things like column names, where “case” is a perfectly suitable column name, but probably not a valid property name without escaping symbols.

  3. I use this enumeration quite often for custom controls
    Enum DisplayType
    End Enum

    I use ReadOnly for controls that should not display the edit button next to them (due to access rights) but since ReadOnly is reserved keyword in VB (not sure about C#) I have to wrap the text in square brackets. Because it is part of the enumeration I never use ReadOnly on it’s own so I do not see a problem with that

  4. I use @variable names to prove a point when im guiding new developers re writing readable code, like the sample on my article on var

    var @this = new @class();

    @base.@stackalloc(delegate(@if @event)


    if (!@return.@bool(@event)) return;

    @is.@const(“S[" +@event + "] matched T[" + @return.@override(@event) + "]“ );



  5. Definitely good information, I just ran into a problem pinvoking some printer properties. I defined a structure to hold all them and noticed that

    Public Default as Boolean
    Public Shared as Boolean

    Would both throw an error, both of these are properties of the Win32_Print class I’m working :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s