“var”是否是?关键字妨碍代码可读性?

发布于 2024-09-26 02:47:31 字数 210 浏览 7 评论 0 原文

我刚刚开始使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帥小哥 2024-10-03 02:47:31

我建议当你有这样的代码时使用 var

var surelyAnXText = new XText(...);

你总是可以告诉我们正在声明变量的想要的类型。但当你有这样的事情时就不是这样了:

var whatIsThis = foo.Some_Method_Where_You_Cant_Tell_The_Return_Type_From_The_Name();

I suggest using var when you have code like this:

var surelyAnXText = new XText(...);

where you always can tell the want type we are declaring a variable of. But not when you have something like this:

var whatIsThis = foo.Some_Method_Where_You_Cant_Tell_The_Return_Type_From_The_Name();
仲春光 2024-10-03 02:47:31

C# 编程指南 建议使用 var当它增强可读性时,例如当类型明显、太复杂或根本不重要时。

var 关键字也很有用
当变量的具体类型
在键盘上打字很乏味,或者
是显而易见的,或者不增加
代码的可读性。一个例子
var 以这种方式有帮助的地方是
具有嵌套泛型类型,例如
用于组操作的那些。在
以下查询的类型
查询变量是
IEnumerable>。只要你和其他人
谁必须维护你的代码理解
这个,使用没有问题
隐式类型以方便和
简洁。

没有一般规则。在某些情况下,显式类型可以增强可读性。

示例:

var x = new Thingy(); //type is obvious

var x = dict.Where(x => x.Value > 3); // type is complex and not important

Foo(GetValue(FromOverThere())); // type is implicit anyway

// equivalent to:
var fromOverThere = FromOverThere();
var value = GetValue(fromOverThere)
Foo(value);

FooDocument doc = repository.Get(id); // glad to see the type here.

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.

The var keyword can also be useful
when the specific type of the variable
is tedious to type on the keyboard, or
is obvious, or does not add to the
readability of the code. One example
where var is helpful in this manner is
with nested generic types such as
those used with group operations. In
the following query, the type of the
query variable is
IEnumerable<IGrouping<string, Student>>. As long as you and others
who must maintain your code understand
this, there is no problem with using
implicit typing for convenience and
brevity.

There is no general rule. There are situations where the explicit type may enhance readability.

Examples:

var x = new Thingy(); //type is obvious

var x = dict.Where(x => x.Value > 3); // type is complex and not important

Foo(GetValue(FromOverThere())); // type is implicit anyway

// equivalent to:
var fromOverThere = FromOverThere();
var value = GetValue(fromOverThere)
Foo(value);

FooDocument doc = repository.Get(id); // glad to see the type here.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文