Using C# Keywords for Variable Names

It’s possible to use C# keywords for variable names, etc.

For example if we wanted a variable called “namespace” (which is a C# keyword) we can prefix the declaration with an @ and use this prefix whenever we refer to the variable:

var @namespace = "hello";

@namespace += " world";

 

If we’re interoperating with another language, for example if we’ve referenced a Visual Basic project from our C# project, and there was a class in the VB project called “abstract”, we could use this in our C# like this:

var a = new @abstract();

a.@foreach = "hello";

a.@foreach += " world";

 

Here the VB class called “abstract” (“abstract” again is a C# keyword) also has a property called “foreach” which is also a C# keyword. Notice the use of the @ in these uses as well.

 

For more C# tips, check out my C# Tips and Traps Pluralsight course.

SHARE:

Add comment

Loading