简体   繁体   English

C#中var和IEnumerable之间的区别?

[英]Difference between var and IEnumerable in C#?

I am reading this article to understand difference between var and IEnumerable. 我读文章,了解VAR和IEnumerable之间的差异。 Author wrote four points like following 作者写了如下四点

  1. Use Var type when you want to make a "custom" type on the fly. 当您想即时创建“自定义”类型时,请使用Var类型。
  2. Use IEnumerable when you already know the type of query result. 当您已经知道查询结果的类型时,请使用IEnumerable。
  3. Var is also good for remote collection. Var也适用于远程收集。
  4. IEnumerable is good for in-memory collection. IEnumerable适用于内存中收集。

point no. 点号 3 & 4 is not clear to me. 我对3和4不清楚。 Please help to understand these point with examples 请通过示例帮助理解这些观点

Everything is wrong here, so briefly at least: 此处的所有内容都不对,因此至少要简短一点:

  • This is wrong, the resultant type will always be the type returned, whether specified explicitly or not; 这是错误的,无论是否显式指定,结果类型将始终是返回的类型。
  • you would need to know the type anyway, if you intend to use the result! 如果您打算使用结果,则无论如何都需要知道类型! var doesn't change that; var不会改变这一点;
  • this third item is pure poppycock; 第三项是纯罂粟; it makes no sense in and of itself; 它本身没有任何意义;
  • IEnumerable generally just does represent an in-memory collection (it doesn't have to, mind, as it's only a contract for collections toi adhere to, regardless of their ultimate storage) and the idea is largely juxtaposed to the rest of this question; IEnumerable通常只是代表内存中的集合(它不就得了,头脑,因为它是唯一的坚持,不管他们最终存储的合同集合TOI)和观念在很大程度上并列这个问题的休息;

A note on usage of var , though, to give a broader set of notions: using var is generally only a good idea when the resultant type is evident based on the current statement - I don't really understand Darren's reasoning or how that would ever be useful, but consider the following: 不过,请注意使用var来给出更广泛的概念:使用var通常仅在根据当前语句得出的结果类型明显时才是一个好主意-我真的不理解Darren的推理或如何很有用,但请考虑以下因素:

var results0 = new List<string>();    // assignment type is obvious 
var results1 = something.GetResult(); // read must investigate to know

var is a contextual keyword that implicitly types a variable. var是上下文关键字,它隐式地键入变量。 When you use IEnumerable<T> you are explicitly stating the type to be defined, were as var will use type inference. 当您使用IEnumerable<T>您将明确说明要定义的类型,因为var将使用类型推断。

It doesn't really make a difference which one you use, more of a coding style IMO, I personally use var when returning something from a collection using LINQ rather than IEnumerable<T> or IQueryable<T> . 使用哪一种并没有真正的区别,更多的是使用IMO的编码样式,当我使用LINQ而不是IEnumerable<T>IQueryable<T>从集合中返回内容时,我个人使用var

var value5 = list.Where(i => i == 5).ToList();
IEnumerable<int> value5 = list.Where(i => i == 5).ToList();

Both will generate the same output. 两者将产生相同的输出。

With regards to point 3 and 4. I don't believe that IEnumerable is any more beneficial for in memory collections at all. 关于第3点和第4点,我认为IEnumerable对内存收集根本没有任何好处。 Behind the scenes the CLR will infer the variables type (When using var) at runtime, most likely to IEnumerable<T> . 在后台, CLR将在运行时推断变量类型(使用var时),最有可能是IEnumerable<T>

var is just an implicitly typed local variable. var只是一个隐式类型的局部变量。 You just let the compiler determine the type. 您只需让编译器确定类型。

var i = 10; // Implicitly typed
int j = 10; // Explicitly typed

It is also super handy when working with anonymous types, eg. 当使用匿名类型时,例如,它也非常方便。

var anon = new { Foo = "Bar" };
Console.WriteLine(anon.Foo); // Puts "Bar" on console.

More information here. 更多信息在这里。

If one says var MyList = SomeStringCollection.ToList() ; 如果有人说var MyList = SomeStringCollection.ToList() ; MyEnumerator = MyList.GetEnumerator(); MyEnumerator = MyList.GetEnumerator(); , then the type of MyList will be List , and the type of MyEnumerator will be List.Enumerator , which is a *value type* that implements IEnumerator , and will thus exhibit *value semantics*. If the type of , then the type of MyList , and the type of will be List , and the type of MyEnumerator , and the type of will be List.Enumerator , which is a *value type* that implements IEnumerator的“ , which is a *value type* that implements , and will thus exhibit *value semantics*. If the type of , and will thus exhibit *value semantics*. If the type of MyList had been IEnumerable , the return type of MyList.GetEnumerator() would have been IEnumerator`, which has reference semantics . , and will thus exhibit *value semantics*. If the type of MyList的, the return type of had been IEnumerable , the return type of MyList.GetEnumerator() , the return type of would have been IEnumerator`,它具有参考语义

This difference could be important if eg one had a method which read five items from an IEnumerator<String> . 如果例如一个方法具有从IEnumerator<String>读取五个项的方法,则这种区别可能很重要。 If one calls such a method multiple times with a variable of type IEnumerator<String> , each call will read the next five items from the enumeration. 如果使用变量IEnumerator<String>多次调用这样的方法,则每次调用都会从枚举中读取下五个项目。 By contrast, if one calls such a method multiple times with a value type like List<String>.Enumerator , each call will copy the enumeration state to a new heap object which implements IEnumerator<T> , pass that object to the method (which will read five items through it), and then abandon that object and its associated space (leaving the enumeration state associated with the original variable unaffected). 相比之下,如果使用值类型如List<String>.Enumerator多次调用这种方法,则每次调用会将枚举状态复制到实现IEnumerator<T>的新堆对象中,然后将该对象传递给方法(将读取其中的五个项目),然后放弃该对象及其关联的空间(不影响与原始变量关联的枚举状态)。

Note that in probably 99% of cases, either value semantics or reference semantics would be equally acceptable in an enumerator; 请注意,在大约99%的情况下,值语义或引用语义在枚举器中同样可以接受; in the majority of those, value semantics would make code run faster, and only very rarely would it make code run meaningfully slower. 在大多数情况下,值语义将使代码运行速度更快,而很少使代码运行速度显着降低。 Further, there are some cases where knowing that an enumerator will behave as a value type could be semantically useful (eg if one wanted to be able to repeatedly re-enumerate part of a collection). 此外,在某些情况下,知道枚举器将充当值类型可能会在语义上有用(例如,如果希望能够重复重新枚举集合的一部分)。 On the other hand, it's probably a good idea for code which uses implementations of IEnumerator<T> to be aware of what kind of implementation they may be using. 另一方面,对于使用IEnumerator<T>实现的代码来了解它们可能使用的实现类型,这可能是一个好主意。

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

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