我刚刚开始使用 Resharper。它的特点之一是它建议基于良好的编码实践对代码进行更改。
它建议的更改之一是在赋值期间将变量类型更改为 var。我不断地改变,现在代码里到处都有 var 。不知何故,我感觉“var”关键字使代码有点难以理解。
尽可能使用“var”是一种良好的编码习惯,还是坚持使用实际类型更好。 (除了需要使用“var”的匿名类型)
谢谢。
I just started using Resharper. One of its features is that it suggests changes to the code based on, i suppose, good coding practices.
One of the changes it suggested is to change the variable type to var during assignment. I kept on changing and now the code has var everywhere. Somehow I get the feeling that "var" keyword makes the code a bit difficult to understand.
Is it a good coding practice to use "var" whereever possible or is it better to stick with the actual type. (except anonymous types where its required to use "var")
Thanks.
发布评论
评论(2)
我建议当你有这样的代码时使用
var
:你总是可以告诉我们正在声明变量的想要的类型。但当你有这样的事情时就不是这样了:
I suggest using
var
when you have code like this:where you always can tell the want type we are declaring a variable of. But not when you have something like this:
C# 编程指南 建议使用 var当它增强可读性时,例如当类型明显、太复杂或根本不重要时。
没有一般规则。在某些情况下,显式类型可以增强可读性。
示例:
The C# programming guide suggest using var when it enhances readability, for instance when the type is obvious, too complicated or not important at all.
There is no general rule. There are situations where the explicit type may enhance readability.
Examples: