How to use C# keywords for Variable names

Occasionally, we might need to use C# keyword for a variable name [which is not recommended].

For example:

int foreach = 20; // This won't work.

int @foreach = 20; // This works.
@foreach =30;

We need to prefix the C# keyword with @ for using it as a variable name. This will be handy, if you are referencing\using a VB.NET project having C# keyword as a variable name within your C# project.
For example, below VB.NET code in a class library works fine:

Public Class foreach
Property int() as Integer
End Class

If you refer this in a C# project, we need to prefix it with @ as shown below:

ClassLibrary2.@foreach obj = new ClassLibrary2.@foreach();
obj.@int = 30;

Ebook Download
View all
View all