简体   繁体   English

c#中类型推断的目的是什么

[英]What is the purpose of Type Inference in c#

I don't see exactly what is the purpose of type inference, what is the benefit from adding that to the language, is there any special use like the in C++11 auto keyword? 我没有确切看到类型推断的目的是什么,将其添加到语言中有什么好处,是否有像C ++ 11 auto关键字那样的特殊用途?

I see it just like an add since I don't know really situation where the resulting type is not necessarily known in advance 我看到它就像添加一样,因为我不真正知道不一定事先知道结果类型的情况

(I mean the use of keyword var) (我的意思是使用关键字var)

Thank you 谢谢

Type inference saves an incredible amount of typing when dealing with complex signatures. 类型推断节省了复杂的签名交易时输入的数量惊人 For instance, 例如,

var itemCreator = _interface.GetItemCreator();

is so much more compact and readable than 比它更紧凑,更易读

Func<int, string, double, decimal, 
    Dictionary<int, KeyValuePair<int, string>>, 
    Action<string, string, string>> itemCreator = _interface.GetItemCreator();

This is obviously an incredibly contrived example designed to prove a point...that code gets messy and the type declarations that go with it are also often messy. 这显然是一个难以置信的人为设计的例子,旨在证明这一点……代码变得混乱,并且随之而来的类型声明也常常混乱。 Type Inference saves us a bit of pain on that front. 类型推断为我们节省了这方面的痛苦。

In addition, Type Inference gives us the ability to use anonymous methods which are used heavily in LINQ and serialization libraries. 另外,类型推断使我们能够使用在LINQ和序列化库中大量使用的匿名方法。

Anonymous methods generate a type at compile time. 匿名方法在编译时生成一个类型。 As a result, there is no way for you to declare the type appropriately ahead of time. 因此,您无法提前声明适当的类型。

As far as your comparison to the auto operator in C++ 11, at first glance it appears to have the same/similar functionality. 就您与C ++ 11中的auto运算符的比较而言,乍一看它似乎具有相同/相似的功能。

public class Class1
{
    public string Method1() 
    {
        return "string";
    }
}

// Calling code
Class1 cls1 = new Class1();
var result = cls1.Method1(); // result is string type.

Now some time later you decide you needed to return int from your Method1. 现在过了一段时间,您决定需要从Method1返回int You can do so without having to worry about the calling code. 您可以这样做,而不必担心调用代码。

Apart from this var is also heavily used in LINQ. 除了这个变种还大量在LINQ使用。

Another use would be to make your code cleaner, for example: 另一个用途是使代码更整洁,例如:

Dictionary<int, string> dictionary = new Dictionary<int, string>();

vs.

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

You can see which one is cleaner to read. 您可以看到哪个更容易阅读。

Type inference helps with at least three aspects of C# development: 类型推断至少有助于C#开发的三个方面:

  1. Writing code 编写代码

    var requires fewer keystrokes, and less time worrying about the exact types returned from methods, etc. var所需的击键次数更少,而为方法等返回的确切类型担心的时间也更少。

  2. Understanding code 了解代码

    var produces more concise, readable code than explicitly specifying types. 与显式指定类型相比, var产生的代码更简洁易读。

  3. Maintaining code 维护代码

    Fewer code changes are needed in fewer places when the types in your application change (and they will). 当应用程序中的类型更改时(并且会更改),需要在较少的地方进行较少的代码更改。

var has many benefits and few drawbacks. var有很多优点,而缺点很少。 I use it everywhere unless I have a compelling reason not to. 除非有充分的理由,否则我会在任何地方使用它。

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

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