为什么我可以创建一个名为&QUOT类; VAR"? [英] Why can I create a class named "var"?
问题描述
不是 VAR
在C#中的关键字?但我为什么可以这样做:
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();
}
}
在 VAR
用于在code是 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.
而当我这样做:
public class int { }
或者这样的:
or this:
public class true { }
编译器说, INT
或真
是一个关键字,不能用这样的。为什么不一样 VAR
?
The compiler said that int
or true
is a keyword and cannot be used like that. Why is it not the same with var
?
推荐答案
VAR
不是关键字的根据此列表。
它是一个上下文关键字,所以从上下文编译器能够决定哪个是你的类,哪个是上下文关键字,并没有引起混淆。
it is a contextual keyword, so from the context the compiler is be able to decide which is your class and which is the contextual keyword, and no confusion arises.
一个上下文关键字是:
用于提供在code进行特定的含义,但它不是一个 保留字在C#。
used to provide a specific meaning in the code, but it is not a reserved word in C#.
,以便它不是保留的,你可以使用它。
so as its not reserved you can use it.
正如在上面有被添加在C#上<一个每个版本的差异以及上下文关键字讨论的各种关键字列表和注释href="http://blogs.msdn.com/b/ericlippert/archive/2009/05/11/reserved-and-contextual-keywords.aspx">Eric Lipperts博客
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 Lipperts blog
有有趣的是注意到,由于组关键字被根据在决定的C#1.0没有出现过的添加,以便preserve向后兼容性。
It is interesting to note that since the set of keywords were decided upon in C#1.0 there have been no additions, so as to preserve backwards compatibility.
这篇关于为什么我可以创建一个名为&QUOT类; VAR&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!