为什么c#需要“var”标识符?
可能的重复:
var 关键字有什么意义?
为什么 c# 需要 " var”类型推断类型变量的标识符?
我的意思是,不写它会有什么问题:
a = 1;
//vs
var a = 1;
ReadingProgramming in Scala:
“类型变量”语法不能简单地忽略类型 - 将不再有标记来开始定义。
但是将其保留在 a:Int 或 int a 中有何不同?
Possible Duplicate:
What's the point of the var keyword?
Why does c# need the "var" identifier for type inferred type variables?
I mean what is the problem with just leaving it off:
a = 1;
//vs
var a = 1;
Reading Programming in Scala:
"Type variable" syntax you cannot simply leave off the type - there would be no marker to start the definition anymore.
But what is the different between leaving it off in the a:Int or int a?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
至少想到的一个原因是,它迫使程序员声明他们引入新变量的意图,而不是分配给现有变量。这使得编译器能够检测到许多常见的编码错误。
At least one reason that comes to mind is that it forces the programmer to declare their intent to introduce a new variable, as opposed to assigning to an existing variable. This enables the compiler to detect numerous common coding errors.
考虑一下:
如果没有
var
关键字,赋值将是给类成员a
。var
明确引入了一个新的局部变量。Consider this:
Without the
var
keyword, the assignment would be to the class membera
.var
introduces unambiguously a new local variable.在 C# 中使用之前必须先定义变量。当然,编译器可以检测到它何时到达
That a had not not be returned ,因此定义该变量并将其赋值为 1 。但是,这可能会导致您遇到的其他问题:
现在您有 2 个变量,您认为你有一个。
You have to define a variable before you use in C#. Sure, the compiler could detect when it reaches
That a had not yet been defined and so define the variable and assign it the value of 1. However, it could lead to other issues where you have:
Now you have 2 variables, where you thought you had one.
来自 MSDN:
这个想法是通过防止意外的隐式类型局部变量。
From MSDN:
The idea is to keep the robustness of C# by preventing accidental Implicitly Typed Local Variable.
从技术上来说,确实没有必要。其他语言允许您将声明和赋值结合起来,无需特殊关键字。
如果
a = 1
对于赋值和声明来说都是有效的语法,那么代码可能会变得非常混乱,我相信这就是为什么 C# 至少需要var
。Technically, there's really no need. Other languages allow you to combine declaration and assignment with no special keywords.
If
a = 1
was valid syntax for both assignment and declaration, though, code could become very confusing which is, I believe, why C# requires at leastvar
.关键字将告诉编译器不要尝试解析方法范围之外的
标识符
。如果没有它,C# 还能工作吗?是的,但这强制执行了强类型语言所需的规则。
keyword will tell the compiler not to try resolve the
identifier
outside the scope of the method.Can C# work without it? Yes, but this enforces a discipline which is required for a strongly typed language.
不同的编程语言被设计来适应不同的目的。 C# 专为编写大型高可靠性软件而设计。其方法的一部分是使用显式声明的静态类型变量。添加匿名对象需要推断变量类型的能力,而匿名对象是 LINQ 查询表达式所必需的。这些东西的存在并没有改变这样一个事实:变量必须显式声明,并且它们的类型在编译时建立(当然,模
动态
)。显式变量声明消除了用不需要变量声明的语言编写的程序中有时会出现的一大类错误。
Different programming languages are designed to suit different purposes. C# is designed for writing large-scale high-reliability software. Part of its approach to this is using explicitly-declared staticly-typed variables. The ability to infer variable types was required by the addition of anonymous objects, which were required for LINQ query expressions. The existance of these things does not alter the fact that variables must be explicity-declared and their types established at compile-time (modulo
dynamic
, of course).Explicit variable declaration removes a large class of bugs that sometimes occur in programs written in languages that do not require variable declarations.
以我的愚见,我认为可以安全地假设他们没有(或不能)如此显着地修改 C# 语法,以至于允许在变量声明中省略(类型或“var”)标记。为语言添加了类型推断。 (这也会对函数声明产生影响。)
我认为这样的更改会对整个语法产生连锁反应,并破坏向后兼容性。
也许 F# 是在如此基础的层面上改变语法的副产品;)
In my humble opinion, I think it is safe to assume that they didn't (or couldn't) modify the C# grammar so significantly as to allow the omission of a (type or "var") token in the variable declaration when they added type inference to the language. (This would have consequences for function declarations also.)
I think such a change would have a ripple effect across the entire grammar and wreck backwards compatibility.
Maybe F# is a by-product of the grammar being changed at such a fundamental level ;)