隐式类型变量
可能的重复:
C# 中 var 关键字的使用
对于 C# 来说相对较新,我想知道MS 必须引入 var
隐式类型变量的动机。文档说:
隐式类型局部变量是强类型的,就像您 自己声明了类型,但编译器确定了类型。
进一步的一些行:
在许多情况下,var 的使用是可选的,并且只是一种语法 方便
这一切都很好,但在我看来,这只会引起混乱。
假设您正在查看这个 for 循环。
foreach (var item in custQuery){
// A bench of code...
}
我不会检查循环的内容和语义,而是会浪费宝贵的时间来寻找项目的类型! 我更喜欢以下内容:
foreach (String item in custQuery){
// A bench of code...
}
问题是:我读到隐式类型变量在处理 LINQ 时有帮助,真的有助于在其他场景中使用它吗?
Possible Duplicate:
Use of var keyword in C#
Being relatively new to C# I was wondering the motivation MS had to introduce the var
implicit typed variables. The documentation says:
An implicitly typed local variable is strongly typed just as if you
had declared the type yourself, but the compiler determines the type.
Some lines further:
In many cases the use of var is optional and is just a syntactic
convenience
This is all nice but in my mind, this will only cause confusion.
Say you are reviewing this for loop.
foreach (var item in custQuery){
// A bench of code...
}
Instead of reviewing the content and the semantics of the loop, I would loose precious time looking for the item's type!
I would prefer the following instead:
foreach (String item in custQuery){
// A bench of code...
}
The question is: I read that implicit typed variables help when dealing with LINQ, does really help to use it in other scenarios?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
引入 LINQ 时需要
var
关键字,以便该语言可以为匿名类型创建强类型变量。示例:
现在
x
是一个具有特定类型的强类型变量,但该类型没有名称。编译器知道xY
的含义,因此您不必使用反射来获取对象中的数据,就像您所做的那样:现在
x
是类型object
,因此您不能使用xY
。例如,当与 LINQ 一起使用时,它可以如下所示:
x
变量现在是IEnumerable
,其中T
是特定类型,没有名字。自从引入
var
关键字以来,它也被用来使代码更具可读性,并被滥用来节省击键次数。使代码更具可读性的一个示例是:
而不是:
这是
var
关键字的一个很好的使用,因为该类型已经存在于语句中。关键字可能被误用的情况是在如下语句中:由于
SomeMethod
名称没有给出任何其返回类型的指示,因此变量的类型并不明显。在这种情况下,您应该写出类型,而不是使用var
关键字。The
var
keyword was needed when LINQ was introduced, so that the language could create a strongly typed variable for an anonymous type.Example:
Now
x
is a strongly typed variable that has a specific type, but there is no name for that type. The compiler knows whatx.Y
means, so you don't have to use reflection to get to the data in the object, as you would if you did:Now
x
is of the typeobject
, so you can't usex.Y
.When used with LINQ it can for example look like this:
The
x
variable is now anIEnumerable<T>
whereT
is a specific type which doesn't have a name.Since the
var
keyword was introduced, it has also been used to make code more readable, and misused to save keystrokes.An example where it makes the code more readable would be:
instead of:
This is a good use of the
var
keyword, as the type already exists in the statement. A case where the keyword can be misused is in a statement like:As the
SomeMethod
name doesn't give any indication of what type it returns, it's not obvious what the type of the variable will be. In this case you should write out the type rather than using thevar
keyword.我认为部分动机是允许这样的东西 -
变成这样 -
第二个示例更短且更具可读性,但仍然清楚地表达了代码的意图。在某些情况下,它会不太清楚,但在很多情况下,你会得到简洁而不失清晰度。
I think some of the motivation was to allow something like this -
to be turned into this -
The second example is shorter and more readable, but still clearly expresses the intent of the code. There are instances when it will be less clear, but in lots of situations you get conciseness with no loss of clarity.
var对于匿名类型来说确实是需要的,在Linq中使用了一下:
由于集合是匿名类型,所以它不能被命名。它有一个真实的类型,但在编译之前没有名称。
var is really needed for anonymous types, which are used in Linq a bit:
Since the collection is of an anonymous type, it can not be named. It has a real type, but not one with a name before compilation.