简体   繁体   English

C# 中类型推断的优缺点是什么?

[英]What are some advantages & disadvantages of type inference in C#?

I have a coworker that is against type inference in C#.我有一个同事反对 C# 中的类型推断。 I believe most of his arguments surrounded lack of readability.我相信他的大部分论点都缺乏可读性。 My argument against that is that Visual Studio's intellisense features provide a simple way of viewing types, and reading them from the code isn't as necessary as it might be if we were coding out of notepad.我的反对意见是,Visual Studio 的智能感知功能提供了一种查看类型的简单方法,并且从代码中读取它们并不像我们在记事本之外编写代码那样必要。

However, I am curious about the advantages and disadvantages of using type inference in C#.但是,我很好奇在 C# 中使用类型推断的优缺点。 I come from C++ and I know that C++0x's 'auto' has a more objective benefit in that you don't always know the types you're getting (especially when doing heavy template programming).我来自 C++,我知道 C++0x 的“自动”有一个更客观的好处,因为你并不总是知道你得到的类型(尤其是在进行大量模板编程时)。 An example is using auto to store the value of Boost.Bind.一个例子是使用 auto 来存储 Boost.Bind 的值。

In C#, type inference doesn't seem to be as much of a requirement so much as it is a "nice to have" or sugar-coating feature.在 C# 中,类型推断的要求似乎并不像它是“很高兴拥有”或糖衣功能那样多。 I think it would be useful for when you are dealing with long types, eg:我认为在处理长类型时会很有用,例如:

Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>> myVar = obj.GetLazy();

it would be:这将是:

var myVar = obj.GetLazy();

This is much cleaner in my opinion.在我看来,这要干净得多。 However, are there any objective arguments for OR against type inference?但是,OR 是否有任何反对类型推断的客观论据? Is it good programming practice to use it, even in situations where it is arguable that it provides no benefit (eg, using 'var' instead of 'int')?使用它是否是一种良好的编程习惯,即使在有争议的情况下它没有提供任何好处(例如,使用“var”而不是“int”)?

Some help in understanding how I should use 'var' in my day-to-day coding would be great.理解我应该如何在日常编码中使用“var”的一些帮助会很棒。

Type inference was invented for exactly the reason you give for C++, you can create anonymous types that don't HAVE a type name (see Lambdas and Linq in particular).类型推断的发明正是出于您为 C++ 提供的原因,您可以创建没有类型名称的匿名类型(特别是请参阅 Lambdas 和 Linq)。

So in that case it's needed.所以在这种情况下是需要的。

In the other case (when the type name is known) then it comes down to style.在另一种情况下(当类型名称已知时)则归结为样式。 I use var when the type is really obvious:当类型非常明显时,我使用var

// I like this - less duplication and easier to read
var item = new List<ComplexObjectItem>();

instead of:代替:

List<ComplexObjectItem> item = new List<ComplexObjectItem>();

Because it reduces duplication.因为它减少了重复。

However, I prefer not to use it when the type is not immediately obvious to the reader:但是,当类型对读者来说不是很明显时,我不喜欢使用它:

// I don't like this - I need to look up what the type is
var item = ResultOfSomeFunctionWhereICantSeeWhatItIs();

But your mileage might vary.但是您的里程可能会有所不同。

Implicit typing can be useful in some cases, and harmful in others.隐式类型在某些情况下很有用,而在其他情况下则有害。 Eric Lippert recently posted an article on the Uses and misuses of implicit typing that is worth a read. Eric Lippert 最近发表了一篇关于隐式类型使用和误用的文章,值得一读。

One thing to remember, var is just for the user, the compiler converts it to its concrete representation when compiling.需要记住的一件事是, var仅供用户使用,编译器在编译时将其转换为其具体表示。

The one downside is when using interfaces from a class.一个缺点是使用类的接口时。

assuming that GetCurrentList() returns a IList<string> :假设GetCurrentList()返回一个IList<string>

IEnumerable<string> list = GetCurrentList();

and

var list = GetCurrentList();

are not the same as in the second example, list will be an IList<string> .与第二个示例不同, list 将是一个IList<string>

I tend to use exlicit typing and usually only use var when it will help the readability of the code and when using anonymous types (because you have to at that point).我倾向于使用显式类型,并且通常仅在有助于代码可读性和使用匿名类型时才使用var (因为此时您必须这样做)。

I think common sense dictates the following informal rules:我认为常识决定了以下非正式规则:

If there's some long name such as :如果有一些长名称,例如:

Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>> myVar = new Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>>();

then replacing it with然后用

var myVar = new Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>>();

makes sense because you can still tell what the object is.是有道理的,因为您仍然可以分辨出对象是什么。

Something ambiguous, on the other hand, might warrant not using var :另一方面,一些模棱两可的东西可能保证不使用var

Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>> myVar = doProcess();
var myVar = obj.GetLazy();

Such type inference, in the presence of intellisense, is a roughly good idea, or okay.在智能感知存在的情况下,这种类型推断是一个大致的好主意,或者可以。 However, if there were no intellisense, then I would not consider it a good idea.但是,如果没有智能感知,那么我不会认为这是一个好主意。 It would be a nightmare instead.相反,这将是一场噩梦。 Without intellisense, I believe most developers would dislike it, due to the inconvenience caused by lack of intellisense (and exact type, both).如果没有智能感知,我相信大多数开发人员会不喜欢它,因为缺乏智能感知(和精确类型,两者)造成的不便。

However, even without intellisense, the following would be good:但是,即使没有智能感知,以下内容也很好:

var obj = new Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>();

In such situations, type inference is relief, as it avoids lots of typing, and duplicate typing!在这种情况下,类型推断是一种解脱,因为它避免了大量输入和重复输入!

With or without intellisense, I prefer to write :有或没有智能感知,我更喜欢写:

Lazy<List<MyNamespace.ISomeVeryLongInterfaceType> obj= obj.GetLazy();

I like to use type inference to make code more concise, however I only use it when I can see what type it is on the same line, eg:我喜欢使用类型推断来使代码更简洁,但是我只在我可以在同一行看到它是什么类型时才使用它,例如:

var myClass = new MyClass();

BUT

MyClass myClass = RandomFuncThatGetsObject();

I think using var in the first example doesn't affect readability, in fact it makes it more readable, however using var in the second example WOULD affect readability.我认为在第一个示例中使用 var 不会影响可读性,实际上它使其更具可读性,但是在第二个示例中使用 var 会影响可读性。

Type inference is necessary when you are working with anonymous types:当您使用匿名类型时,类型推断是必要的:

var x = new { Greeting = "Hello", Name = "World" };

When you use LINQ queries, you usually use anonymous types all the time.使用 LINQ 查询时,通常始终使用匿名类型。

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

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