Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to use reserved words as member names.

0.00/5 (No votes)
25 Jun 2011 1  
This tip shows one way to use reserved words as member names using verbatim identifiers.

Disclaimer: I do not condone the use of this tip. I just find it interesting.


Today, I was using ReSharper to rename some constants in a class. The constants were named inch, pica, and point. I wanted to rename them to in, pc, and pt. Resharper caught the obvious error in my thinking which is that the name in is a reserved keyword in C# (duh).


What I found interesting is that ReSharper didn't go ahead and follow my instructions (thanks!) or tell me I was being dumb for trying to name something using the same name as the reserved word; it renamed it to @in.


Huh. A quick search revealed that yes indeed, any member can begin with the @ character. You learn something new every day. Therefore this code is valid:


C#
public class @foo
{
    private string @baa = "sheep";

    private void @black()
    {
        Console.Write("Hello" + @sheep);
    }
}

I also noted that even Intellisense doesn't correctly colour-code when @ is used to prefix a class' name.



Follow-up: Apparently they are called verbatim identifiers. Thanks SubsonicDesignOfficial.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here