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.


Comments (1) -

  • Jim Keifer

    10/21/2019 12:40:56 AM | Reply

    I was wondering if there was a way to include the datatypes that can have MinValue,MaxValue and sizeof together in some sort of collection, so that I could iterate through them with a foreach loop instead of 33 lines of WriteLine code?

Add comment