什么是&QUOT的重要性; VAR"关键字在C#.NET? [英] What is the significance of "var" keyword in c#.Net?

查看:150
本文介绍了什么是&QUOT的重要性; VAR"关键字在C#.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
在C#中结果
什么是var关键字的地步?

我有点困惑在这里使用了显式类型隐式类型

I am a bit confused here in using Implicit typing over Explicit typing.

在C#中引述MSDN编码标准:

As quoted by MSDN in C# Coding Standards:

使用局部变量隐式类型时变量
的类型是显而易见的分配的右边时,或当精确
类型并不重要。

Use implicit typing for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important.

我的问题是,当我们已经知道的基本变量类型,为什么我们需要在这里使用隐式类型?

My question is, when we already know the underlying variable type, why do we need to use implicit typing here?

会是怎样的优势呢?

请帮助澄清我的疑问。

推荐答案

以此为例:

var q = from c in myCollection
        select new
    { c.Value1, c.Value2 };

我们无法确定q这里的数据类型,因为它在运行时anonymus型的产生结果
,这是它的主要目的

we can't determine the datatype of q here because it's generated at run time "anonymus-type"
and that is the main purpose of it

这篇关于什么是&QUOT的重要性; VAR"关键字在C#.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆