C# 3.0 中的 var 关键字
Possible Duplicate:
What’s the point of the var keyword?
Hello everyone,
I want to confirm whether my understanding is correct. If I do not use LINQ, then the only benefit of using var is to make brevity? Is that correct understanding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您不使用 LINQ,var 只允许您声明变量的类型一次,而不是两次。
示例
vs
这只能在本地完成,对于声明匿名类型也很有用。
例子
If you're not using LINQ, var allows you to only declare the type of the variable once, instead of twice.
Example
vs
This can only be done locally, and is also useful for declaring anonymous types.
Example
除了 LINQ 查询之外,我在使用 var 关键字时会非常谨慎。 在某些特定情况下,您只需要匿名类型,但我认为这种情况很少见。 Var 可能会导致非常混乱的代码,因为除非您使用智能感知拐杖,否则您在阅读代码时不知道正在处理的类型是什么。
让我越来越担心的是,我看到这么多的代码片段和代码位执行以下操作...它很懒,而不是 var 关键字的目的:
所以将它用于 LINQ...将它用于匿名类型(如果如果您确实在 LINQ 之外使用了匿名类型,那么您应该仔细检查为什么需要这样做)。
引用 MSDN (文章的最后一行)有关 var 使用的内容:
不要将其用作节省击键的捷径,下一个查看您代码的人会欣赏它。
Other than for LINQ queries I would be very cautious in using the var keyword. There are specific instance when you just need an anonymous type but this is few and far between I think. Var can lead to very confusing code as you have no idea what the type you are dealing with when reading the code unless you use the intellisense crutch.
It worries me more and more that I see so many snippets and bits of code that do the following... it's lazy and not what the var keyword was intended for:
So use it for LINQ... use it for anonymous types (if you do use anonymous types outside of LINQ you should really scrutinize why you need to).
Quote from MSDN (very last line of article) regarding use of var:
Don't use it as a short cut to save keystrokes, the next guy looking at your code will appreciate it.
差不多,是的。
var
可以用在编译器可以从您分配给变量的任何值推断变量类型的地方。 (但是,类型推断规则相当复杂,因此您可能需要阅读 C# 规范才能完全理解。)这并不完全正确,因为需要
var
关键字用于定义匿名类型。 例如:当然,它可以在 LINQ 查询之外使用,也可以在内部使用。
Pretty much, yes.
var
may be used wherever the compiler can infer the type of the variable from whatever value you are assigning to it. (The type inference rules are quite complex however, so you may want to read the C# specification for a full understandin.)It's not quite correct in that the
var
keyword is required for defining anonymous types. For example:which can be used outside of LINQ queries as well as inside, of course.
我不认为使用 var 应该是一个问题 - 而且我更喜欢它正是因为代码可读性的原因。 首先,var 只是语法糖,并且在发出 IL 时被编译为正确的类型。 就代码可读性而言,关注变量的用途以及如何分配它而不仅仅是其类型更有意义。 VS .NET 编辑器无论如何都会在其后面的行中显示类型 - 如果您将鼠标悬停在其上。 所以这根本不应该是一个问题。 就调试而言 - 如果您看到“自动/本地/监视”窗口 - 它们会显示所有成员的类型。
对我来说,看到这样的代码更有意义:
与 var 相反,
它的公平性仅限于在局部变量声明中使用,而局部变量声明也在声明时初始化。 在这种情况下,如果省略实际类型,在我看来肯定会提高可读性。
编辑:对我来说,不警告以下用法是不公平的:
这不好; 当文字适用于多种类型时,您需要知道文字的默认类型,从而了解 x 的类型推断出的内容。 是的,无论如何,我会避免这样的声明。
I don't think using var should be a problem - and I prefer it for exactly the reasons of code readability. First of all, var is only syntactic sugar and just gets compiled away to a proper type when IL is emitted. And as far as the code readability goes, it makes more sense to focus on the purpose the variable is used for, and how it is assigned than just its type. VS .NET editor shows the type in the line following it anyway - if you just hover on it. So this shouldn't be a problem at all. And as far as the debugging goes - if you see Autos/Local/Watch windows - they display the types of all the members.
It makes more sense for me to see code like this:
as opposed to
var is in its fairness limited to using in local variable declarations which are also initialized at the time of declaration. And in that one case, if you omit the actual type it definitely improves readability IMO.
EDIT: And it would unfair on my part not to warn against the usages as below:
This is not good; when the literal is applicable to multiple types, you need to know the default type of the literal and hence understand what is infered for the type of x. Yes, by all means, I would avoid such declarations.
我相信在处理通过 Web 服务等获取的数据时,它也被用在 WCF(Windows 通信基金会)中。
I believe it is also used in the WCF (Windows communication Foundation) when dealing with data obtained via webservices and the like.
我还发现使用 var 还可以简化低耦合设计中的重构。 这是因为我们倾向于强类型变量,但通常后面的代码需要较弱的类型。 使用 var 可以抵消编译器的类型更改。
I've also found that the use of var also eases refactoring in low-coupled designs. This is because we tend to strong type variables, but normally the code that follows is expecting weaker types. Using var you'll offset type changes to the compiler.
不,您可以使用
var
构造匿名类型< /a>,无论您是否使用 LINQ:No, you can use
var
to construct anonymous types, regardless of whether or not you're using LINQ:使用这样的类型也更容易。 当泛型类型很长时,类型名称可能会妨碍将变量名称直观地识别为声明的一部分。
特别是如果你返回并将其更改为
It's also easier for working with types like this. When you have very long generic types, the type name can get in the way of visually identifying the variable name as part of a declaration.
especially if you go back through and change it to
来自msdn:
MSDN 链接
From msdn:
MSDN Link Here