Using reserved Keywords as variables in C#

Sometimes you may want to use any C# specific keyword into your code. That is definitely not allowed but there may arise a need of such thing. I will give a scenario for that where you may need such thing but lets explore this with an example first.

Let us say you want to use a variable named namespace in your C# code.


As you can see it gives you a syntax error that you are not allowed to use the C# reserved keywords. But you may need to use it. So lets do it this way. You just prefix an @ symbol to that variable. It will accept it as a variable. In the following code you can see the value in debug mode.


Now where can you possibly use this feature. Well one scenario that I know is that you may use a library from any other .Net framework language such as VB.Net. And you don’t have same reserved keywords in VB.Net. So a person who has created the library in VB.Net may have used any variable with the name that is a reserved keyword in C#. Let us see the following example.


The above code is a class from a class library written in VB.Net. Compile it and add a reference of that library to your C# project. Note in this code that name of the Class is a reserved keyword in C#. Also the method name is a reserved keyword for C#. So when you include this library to your C# project how would you use it. Same @ symbol is there on rescue.


That’s why I love C#. This is one use of this feature that I know. Please share any other possible use of this feature that you know or even if you have any interesting feature to share kindly share. If you think it is a useful feature and interesting knowledge, kindly spread it to others. That’s all for today folks. See you tomorrow. 🙂

  • Show Comments (0)

Your email address will not be published. Required fields are marked *

comment *

  • name *

  • email *

  • website *