Why can I create a class named “var”?

后端 未结 5 1862
-上瘾入骨i
-上瘾入骨i 2020-12-05 09:22

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)
    {
           


        
5条回答
  •  粉色の甜心
    2020-12-05 09:37

    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.

提交回复
热议问题