Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Tip/Trick

How to use reserved words as member names.

Rate me:
Please Sign up or sign in to vote.
4.83/5 (8 votes)
25 Jun 2011CPOL 20.1K   1   6
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Robotic Assistance Devices / AITX
Canada Canada
Yvan Rodrigues has 30 years of experience in information systems and software development for the industry. He is Senior Concept Designer at Robotic Assistance Devices

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., Mabel's Labels Inc. as Manager of Systems and Development, the University of Waterloo as Information Systems Manager, and OTTO Motors as Senior Systems Engineer and Senior Concept Designer.

Yvan is currently focused on design of embedded systems.

Comments and Discussions

 
GeneralReason for my vote of 3 OK, yes, you can do it. But why is R... Pin
Br.Bill29-Jun-11 7:17
Br.Bill29-Jun-11 7:17 
GeneralNice, I learned something. Pin
Dr.Walt Fair, PE25-Jun-11 15:43
professionalDr.Walt Fair, PE25-Jun-11 15:43 
GeneralReason for my vote of 5 very good trick. Indeed a trick! 5! Pin
JP_Rocks23-Jun-11 1:18
JP_Rocks23-Jun-11 1:18 
QuestionInteresting Pin
SubsonicDesignOfficial21-Jun-11 8:48
SubsonicDesignOfficial21-Jun-11 8:48 
AnswerRe: Interesting Pin
Yvan Rodrigues21-Jun-11 9:09
professionalYvan Rodrigues21-Jun-11 9:09 
GeneralRe: Interesting Pin
SubsonicDesignOfficial21-Jun-11 9:10
SubsonicDesignOfficial21-Jun-11 9:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.