English 中文(简体)
C#中那种推论有什么好处和缺点?
原标题:What are some advantages & disadvantages of type inference in C#?

我有一位同事反对C#中的那种推论。 我认为,他的大多数论点涉及的是缺乏可读性。 我反对的理由是,演播室视觉星座的特征提供了一种简单的观察类型方法,如果我们脱下笔记,从密码中读到这些类型是必要的。

然而,我很想知道在C#中采用类型推论的利弊。 我来自C++,我知道C++0x汽车具有更客观的效益,因为你不总是知道你重新获得的类型(特别是在进行重模板编制时)。 其中一个例子是利用汽车储存布埃斯特的价值。 页: 1

在C#,类推似乎与“有”或食糖特征一样多。 我认为,如果你处理长期问题,例如:

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

将:

var myVar = obj.GetLazy();

我认为,这更为清洁。 然而,是否有任何客观理由反对任意干涉? 即便在有理由认为没有好处的情况下(例如,使用var而不采用 in)采用这种做法,是否好?

有些人帮助了解我如何在日常的编码中使用var将非常大。

最佳回答

具体来说,由于你给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:

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

而不是:

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

因为它减少了重复。

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.

问题回答

我认为,共同的含义决定了以下非正式规则:

如果有长名,例如:

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:

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

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.

One thing to remember, var is just for the user, the compiler converts it to its concrete representation when compiling.

The one downside is when using interfaces from a class.

assuming that GetCurrentList() returns a 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>.

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).

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, e.g:

var myClass = new MyClass();

BUT

MyClass myClass = RandomFuncThatGetsObject();

我认为,首先使用斜体不会影响可读性,事实上,它使可读性更强,但第二种情况下使用斜体会影响可读性。

在你从事匿名工作时,需要打字:

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

当你使用准则查询时,你通常使用匿名类型。

var myVar = obj.GetLazy();

在有线人的情况下,这种推论是一种相当好的想法,或者是一种骗局。 然而,如果没有智慧,我就不认为这是一个好的想法。 这将是一场恶梦。 如果没有智慧,我认为大多数发展者会不喜欢,因为缺乏智慧(和准确的类型)造成了不便。

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!

在没有律师的情况下,我倾向于写:

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签