隐式类型变量

发布于 2024-12-18 09:28:40 字数 736 浏览 0 评论 0原文

可能的重复:
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 技术交流群。

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

发布评论

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

评论(3

微暖i 2024-12-25 09:28:40

引入 LINQ 时需要 var 关键字,以便该语言可以为匿名类型创建强类型变量。

示例:

var x = new { Y = 42 };

现在 x 是一个具有特定类型的强类型变量,但该类型没有名称。编译器知道 xY 的含义,因此您不必使用反射来获取对象中的数据,就像您所做的那样:

object x = new { Y = 42 };

现在 x 是类型 object,因此您不能使用 xY

例如,当与 LINQ 一起使用时,它可以如下所示:

var x = from item in source select new { X = item.X, Y = item.Y };

x 变量现在是 IEnumerable,其中 T 是特定类型,没有名字。

自从引入 var 关键字以来,它也被用来使代码更具可读性,并被滥用来节省击键次数。

使代码更具可读性的一个示例是:

var list =
  new System.Collections.Generic.List<System.Windows.Forms.Message.Msg>();

而不是:

System.Collections.Generic.List<System.Windows.Forms.Message.Msg> list =
  new System.Collections.Generic.List<System.Windows.Forms.Message.Msg>();

这是 var 关键字的一个很好的使用,因为该类型已经存在于语句中。关键字可能被误用的情况是在如下语句中:

var result = SomeMethod();

由于 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:

var x = new { Y = 42 };

Now x is a strongly typed variable that has a specific type, but there is no name for that type. The compiler knows what x.Y means, so you don't have to use reflection to get to the data in the object, as you would if you did:

object x = new { Y = 42 };

Now x is of the type object, so you can't use x.Y.

When used with LINQ it can for example look like this:

var x = from item in source select new { X = item.X, Y = item.Y };

The x variable is now an IEnumerable<T> where T 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:

var list =
  new System.Collections.Generic.List<System.Windows.Forms.Message.Msg>();

instead of:

System.Collections.Generic.List<System.Windows.Forms.Message.Msg> list =
  new System.Collections.Generic.List<System.Windows.Forms.Message.Msg>();

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:

var result = SomeMethod();

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 the var keyword.

拧巴小姐 2024-12-25 09:28:40

我认为部分动机是允许这样的东西 -

List<int> list = new List<int>();

变成这样 -

var list = new List<int>();

第二个示例更短且更具可读性,但仍然清楚地表达了代码的意图。在某些情况下,它会不太清楚,但在很多情况下,你会得到简洁而不失清晰度。

I think some of the motivation was to allow something like this -

List<int> list = new List<int>();

to be turned into this -

var list = new List<int>();

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.

凡尘雨 2024-12-25 09:28:40

var对于匿名类型来说确实是需要的,在Linq中使用了一下:

var results =
    from item in context.Table
    select new {Name=item.Name, id=item.id};

由于集合是匿名类型,所以它不能被命名。它一个真实的类型,但在编译之前没有名称。

var is really needed for anonymous types, which are used in Linq a bit:

var results =
    from item in context.Table
    select new {Name=item.Name, id=item.id};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文