Why can I create a class named "var"?

C#.Net 4.0

C# Problem Overview


Isn't var a keyword in C#? But why can I do this:

public class var { }

public class main
{
    public static void main(string[] args)
    {
        var testVar = new var();
    }
}

The var that is used in the code is the var class that is declared before the main class. And the compiler doesn't even complain.

While when I do this:

public class int { }

or this:

public class true { }

The compiler said that int or true is a keyword and cannot be used like that. Why is it not the same with var?

C# Solutions


Solution 1 - C#

var is not a keyword according to this list.

it is a contextual keyword, so from the context, the compiler is able to decide which is your class and which is the contextual keyword, and no confusion arises.

a contextual keyword is:

> used to provide a specific meaning in the code, but it is not a > reserved word in C#.

so as it's not reserved you can use it.

As pointed out in the comments above there is a discussion of the differences as well as a list of the various keywords and contextual keywords added at each version of c# on Eric Lippert's blog

It is interesting to note that since the set of keywords was decided upon in C#1.0 there have been no additions, so as to preserve backward compatibility.

Solution 2 - C#

The compiler is smart enough to know that the context you are using var as a class name is never a context for the keyword so allows it (which is why it is defined as a contextual keyword).

Solution 3 - C#

Another way of looking at this: "var" as a keyword was not in the first versions of C# (unlike "int" and "true"), so you might have written some code then which had a class called "var". This was perfectly fine and legal. Then later on when "var" was added to the language, the designers were so kind as to make it a keyword only in certain contexts, so your existing var class would still work.

This is one of the real challenges of language design - how to add new features without breaking existing code, and without making the new features cumbersome to use.

Solution 4 - C#

In C# versions before version 3, implicitly typed local variables were not supported yet, so var had no special meaning and it was possible to define variables and classes named var. Your example program is legal, because both occurrences of var in main referred to the class var.

C# 3 and later versions are downwards compatible, so code written in C# before version 3 still compiles with the new compilers.

int and true are keywords since C# 1.

Solution 5 - C#

Keywords can be reserved contextually. When the source code is parsed, that context is established as part of the parse tree. The evaluation of keywords takes place within that context. So, in this case, var isn't in a reserved context and won't have the same meaning as when you're using it in an assignment statement. I believe one reason for this flexibility is that var was introduced in C# 3, so making it reserved everywhere might have broken backwards compatibility in some programs, whereas using it as a variable type declaration wouldn't have compiled in earlier versions, so there's no breakage.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn Isaiah CarmonaView Question on Stackoverflow
Solution 1 - C#Sam HolderView Answer on Stackoverflow
Solution 2 - C#OdedView Answer on Stackoverflow
Solution 3 - C#PolyfunView Answer on Stackoverflow
Solution 4 - C#dtbView Answer on Stackoverflow
Solution 5 - C#pfriesView Answer on Stackoverflow