Why can I create a class named “var”?

后端 未结 5 1857
-上瘾入骨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:48

    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.

提交回复
热议问题