简体   繁体   English

在C#3.0> +中隐式输入有什么好处

[英]What are the benefits of implicit typing in C# 3.0 >+

The only advantage I can see to do: 我能看到的唯一优势:

var s = new ClassA();

over 过度

ClassA s = new ClassA();

Is that later if you decide you want ClassB, you only have to change the RHS of the declaration. 如果你决定要ClassB,那么你只需要更改声明的RHS即可。

I guess if you are enumerating through a collection you can also just to 'var' and then figure out the type later. 我想如果你通过集合枚举,你也可以只是'var',然后再计算出类型。

Is that it?? 是吗? Is there some other huge benefit my feeble mind does not see? 还有一些其他巨大的好处,我的虚弱的头脑没有看到?

It's mostly syntactic sugar. 它主要是语法糖。 It's really your preference. 这是你的偏好。 Unless when using anonymous types, then using var is required. 除非使用匿名类型,否则需要使用var。 I prefer implicit typing wherever possible though, it really shines with LINQ. 尽管如此,我更喜欢隐式打字,它真的很流行于LINQ。

I find it redundant to type out a type twice. 我发现输入两次类型是多余的。

List<string> Foo = new List<string>();

When I can easily just type var when it's obvious what the type is. 当我明显知道类型是什么时,我可以很容易地输入var。

var Foo = new List<string>();

var is useful for anonymous types, which do not have names for you to use. var对于匿名类型很有用,它们没有您可以使用的名称。

var point = new {X = 10, Y = 10};

This will create an anonymous type with properties X and Y. It's primarily used to support LINQ though. 这将创建一个具有属性X和Y的匿名类型。它主要用于支持LINQ。 Suppose you have: 假设你有:

class Person
{
    public String Name {get; set;}
    public Int32 Age {get; set;}
    public String Address {get; set;}
    // Many other fields
}

List<Person> people; // Some list of people

Now suppose I want to select only the names and years until age 18 of those people who are under the age of 18: 现在假设我只想选择年龄在18岁以下的人的年龄18岁的名字和年份:

var minors = from person in people where person.Age < 18 select new {Name = person.Name, YearsLeft = 18 - person.Age};

Now minors contains a List of some anonymous type. 现在, minors包含一些匿名类型的List We can iterate those people with: 我们可以通过以下方式迭代这些人:

foreach (var minor in minors)
{
    Console.WriteLine("{0} is {1} years away from age 18!", minor.Name, minor.YearsLeft);
}

None of this would otherwise be possible; 否则这些都不可能; we would need to select the whole Person object and then calculate YearsLeft in our loop, which isn't what we want. 我们需要选择整个Person对象,然后在循环中计算YearsLeft,这不是我们想要的。

I started what turned out to be a hugely controversial thread when I first signed on here (my choice of "evilness" to describe general use of var was obviously a terrible choice.) Needless to say, I have more appreciation for var than I did before I started that thread, as well as a better understanding of how it can be usefully used: 当我第一次在这里签名时,我开始发现了一个极具争议性的话题(我选择“邪恶”来描述var的一般用法显然是一个糟糕的选择。)不用说,我对var的理解比我做的更多。在我开始该线程之前,以及更好地理解它如何被有效地使用:

The evilness of 'var' in C#? C#中'var'的邪恶?

Some good reasons to use var: 使用var的一些好理由:

  • Brevity 简短
  • Reduction of Repetition (DRY) 减少重复(DRY)
  • Reduced refactoring effort 减少重构工作量
  • Supports anonymous types (key reason it was added to C#) 支持匿名类型(它被添加到C#的关键原因)

Have a look at this questions. 看看这个问题。 Maybe they'll help you decide. 也许他们会帮你决定。

Use of var keyword in C# 在C#中使用var关键字
https://stackoverflow.com/questions/633474/c-do-you-use-var https://stackoverflow.com/questions/633474/c-do-you-use-var

It allows me to not repeat myself unnecessary. 它让我不再重复自己的不必要了。 Consider this: 考虑一下:

Dictionary<string, List<int>> dict = new Dictionary<string, List<int>>();

We have a very long typename repeated twice on the same line twice with absolutely no benefit. 我们有一个非常长的类型名称在同一行重复两次,绝对没有任何好处。 Furthermore, if you ever need to refactor this, you'll need to update the type twice. 此外,如果您需要重构此内容,则需要两次更新类型。 Whereas this is just as expressive: 虽然这同样具有表现力:

var dict = new Dictionary<string, List<int>>();

There's still no doubt about type of dict here, but the code is shorter, and I would claim that it is easier to read as well. 这里仍然没有关于dict类型的疑问,但代码更短,我声称它也更容易阅读。

Actually "var" in C# is used to deal with anonymous type. 实际上C#中的“var”用于处理匿名类型。

Like: 喜欢:

var t = new {number = 10, name = "test"};

The overarching reason for the implicit typing was to support anonymous types, like those produced by LINQ queries. 隐式类型的首要原因是支持匿名类型,如LINQ查询生成的类型。 They can also be a big benefit when dealing with complex-typed generics...something like Dictionary<int,List<string>> . 在处理复杂类型的泛型时,它们也是一个很大的好处......比如Dictionary<int,List<string>> That's not too complex, but imagine enumerating... 这不是太复杂,但想象枚举......

foreach KeyValuePair<int,List<string>> pair in myDictionary
{

}

is simplified with implicit typing. 隐式输入简化了。

While the var keyword was primarily introduced to support anonymous types, the main argument I've seen for using it more widely is for brevity/more readable code, plus fitting more on your line of code: 虽然var关键字主要是为了支持匿名类型而引入的,但我更广泛地使用它的主要论点是为了简洁/更易读的代码,再加上你的代码行更多:

Dictionary<string, double> data = new Dictionary<string, double>();
versus
var data = new Dictionary<string, double>();

Though not a good reason for most people, I also like the var keyword as a blind programmer, as I listen to the code being read out and thus here the variable name after just one syllabul :-) 虽然对大多数人来说不是一个好理由,但我也喜欢var关键字作为盲人程序员,因为我听了正在读出的代码,因此这里只是一个syllabul之后的变量名:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM