var:类型或关键字
MSDN 将 var
分类在 下类型
。
在方法中声明的变量 作用域可以有一个隐式类型 var
在此上下文中“隐式类型 var”意味着什么?
严格地说,如果我有能力向其他程序员解释的话。
我可以说吗? var 是一个类型,或者我必须说; var 是一个关键字,它指示编译器确定类型本身。
注意:这并不是为了开始讨论 var,也不是为了学习 var 的使用。我想一劳永逸地知道如何准确地描述它,msdn 有点令人困惑,仅此而已。
MSDN categorizes var
under Types
.
variables that are declared at method
scope can have an implicit type var
what does 'implicit type var' mean in this context?
Strictly said, if I have it to explain to fellow programmers.
Can I say; var is a Type, or do I have to say; var is a keyword that instructs the compiler to determine the type itself.
note: this is not meant to start a discussion about var, nor to learn the use of var. For once and for all I want to know excactly how to describe it and msdn is a bit confusing, that's it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
var
是上下文关键字 - 例如,还有yield
、add
和get
。换句话说,您可以将其用作标识符,而无需在其前面添加 @ 前缀,但在某些地方(即局部变量声明需要类型名称的地方),它对编译器仍然具有特殊含义。
使用
var
声明局部变量会要求编译器根据右侧的表达式推断变量的类型。例如:list
的类型为List
;anon
的类型是匿名类型,也在 C# 3 中引入。在 C# 3 中引入var
的部分原因是为了允许使用匿名类型的强类型变量 - 即使您无法显式声明该类型,该变量仍然具有适当的编译时类型。但是,如果编译器没有足够的信息,则在某些情况下
var
不起作用:还有其他情况。在每种情况下,您都可以将表达式强制转换到右侧,以便编译器知道要使用什么 - 但在这种情况下,您也可以只显式声明变量。
<插头>
您可以在C# 深入了解中了解更多相关信息。幸运的是,涵盖这一点的章节仍然可以从第一版页面免费获得(您需要第8章)。我不记得在第二版中我对这一章做了多少修改...
var
is a contextual keyword - along withyield
,add
andget
for example.In other words, you can use it as an identifer without prefixing it with @, but it still has a special meaning to the compiler in some places (i.e. where a type name is expected for a local variable declaration).
Using
var
to declare a local variable asks the compiler to infer the type of the variable based on the expression on the right hand side. For example:The type of
list
isList<string>
; the type ofanon
is an anonymous type, also introduced in C# 3. Part of the reason for introducingvar
in C# 3 was to allow for strongly typed variables using anonymous types - the variable still has the appropriate compile-time type, even though you couldn't explicitly state that type.There are a few cases where
var
doesn't work, however, if the compiler doesn't have enough information:There are others too. In each case you could just cast the expression on the right-hand side, so that the compiler knew what to use - but in that case you might as well just declare the variable explicitly instead.
<plug>
You can read more about this in C# in Depth. Fortunately, the chapter covering this is still available for free from the first edition page (you want chapter 8). I can't remember how much I've changed this chapter in the second edition...
</plug>
这取决于你如何看待它。严重地!
C# 编译器对程序进行三种分析,每个后续分析都会消耗前一个分析的结果。我们做的第一种分析是词法分析;我们将文本转化为一系列标记。假设您
首先确定这些标记是 CLASS IDENTIFIER(C) LEFTBRACE STATIC VOID IDENTIFIER(Main) LEFTPAREN RIGHTPAREN LEFTBRACE IDENTIFIERTHATMIGHTBEAKEYWORD(var) IDENTIFIER(x) EQUALS INTEGER(123) SEMICOLON RIGHTBRACE RIGHTBRACE。
在这个分析阶段,“var”是一个标识符,上面有一个注释,表明它可能是关键字 var,而不是标识符 var。
我们要做的下一个分析是语法分析,其中标记被组织成组,形成“解析树”。在这种情况下,标记将被组织到这棵树中(缩进表示树中的嵌套级别)
此时,从语法上来说,var 已被分类为一种类型。如果这是一个语法上合法的程序,它就在我们期望类型声明的地方。
但此时我们不知道“var”是指类型还是被用作关键字。这需要语义分析。
当我们对解析树进行语义分析时,我们会说“好吧,有一个类 C,它有一个方法 Main,它声明了一个局部变量 x。该局部变量的类型是什么?”
此时有两种可能。第一种可能性是全局命名空间中存在名为“var”的类、结构、枚举、接口或委托类型。 (此程序片段中没有“using”指令,因此我们不必检查任何其他名称空间,并且类型不是部分的,因此我们不必检查任何其他类型声明)。我们搜索所有引用程序集的全局命名空间,寻找名为“var”的类型。我们还对所有其他文件中的类型声明进行语义分析,再次寻找名为 var 的类型。
如果我们找到一个,那么“var”就是指该类型。
如果我们没有找到,那么我们假设“var”被用作引入隐式类型局部变量的上下文关键字。
因此,为了回答您的问题:
从词法上来说,代码片段“var”是一个标识符标记,上面有一个注释,上面写着“顺便说一句,这实际上可能是一个关键字”。
从语法上来看,当标记“var”出现在语法中需要类型的位置时,它就是一种类型。
从语义上来看,当作用域中存在名为 var 的类型时,类型语法“var”就是一个类型;如果没有这样的类型并且“var”被用作局部声明的类型,则类型语法“var”不引用类型。相反,它是一条指示编译器推断本地实际类型的指令。
It depends on how you look at it. Seriously!
The C# compiler does three kinds of analysis on programs, each subsequent analysis consuming the result of the previous one. The first kind of analysis we do is lexical analysis; we take the text and turn it into a series of tokens. Suppose you have
We first determine that those tokens are CLASS IDENTIFIER(C) LEFTBRACE STATIC VOID IDENTIFIER(Main) LEFTPAREN RIGHTPAREN LEFTBRACE IDENTIFIERTHATMIGHTBEAKEYWORD(var) IDENTIFIER(x) EQUALS INTEGER(123) SEMICOLON RIGHTBRACE RIGHTBRACE.
At this stage of analysis, "var" is an identifier with a note on it that says that it might be the keyword var, not the identifier var.
The next analysis we do is a grammatical analysis where the tokens are organized into groups that form a "parse tree". In this case the tokens would be organized into this tree (indentation indicates nesting level in the tree)
At this point var has been classified as a type, grammatically. It it in the place we would expect a type declaration if this is a grammatically legal program.
But at this point we do not know whether "var" refers to a type or is being used as a keyword. That requires semantic analysis.
When we do the semantic analysis of the parse tree we say "OK, there's a class C, it's got a method Main, which declares a local variable x. What is the type of that local variable?"
At this point there are two possibilities. The first possibility is that there is a class, struct, enum, interface or delegate type named "var" in the global namespace. (There are no "using" directives in this program fragment so we don't have to check any other namespaces, and the type is not partial so we don't have to check any other type declarations). We search the global namespace of all the referenced assemblies looking for a type called "var". We also do semantic analysis of the type declarations in all other files, again, looking for a type called var.
If we find one, then "var" refers to that type.
If we don't find one then we assume that "var" is being used as a contextual keyword that introduces an implicitly typed local.
So, to answer your question:
Lexically, the code fragment "var" is an identifier token with a note on it that says "by the way, this might actually be a keyword".
Grammatically, the token "var" is a type when it appears at a position in the grammar where a type is expected.
Semantically, the type syntax "var" is a type when there is a type in scope called var; if there is no such type and "var" is being used as the type of a local declaration then the type syntax "var" does not refer to a type. Rather, it is an instruction to the compiler to infer the actual type of the local.
两者兼而有之。从技术上讲,使用 var 时,变量是强类型的,但编译器确定类型。所以我想说,当你说“var 是一种类型”时,你会最接近。
It's a bit of both. Technically, when using var, the variable is strongly typed, but the compiler determines the type. So I would say you'd be closest when saying 'var is a type'.
var 是一个类型,就像 int 一样......但是它到底是什么类型?
好吧,编译器根据该行的其余部分为您计算出这一点(这就是隐含的含义)。
例如
,在编译时,var 将替换为string。
var是作为程序员方便的简写,而不是必须要写,
程序员可以写
和它的意思是一样的。
var is a Type, like int is ... but what Type is it exactly??
Well, the compiler works that out for you (which is what implicitly means) based on the rest of the line.
e.g.
At compile time, var will be replaced with string.
var is used as a convenient short hand for the programmer, instead of having to write
a programmer can write
and it means the same thing.