ienumerable

Why does IEnumerable<T> inherit from IEnumerable?

匆匆过客 提交于 2019-12-17 06:37:30
问题 This might be a old question: Why does IEnumerable<T> inherit from IEnumerable ? This is how .NET do, but it brings a little trouble. Every time I write a class implements IEumerable<T> , I have to write two GetEnumerator() functions, one for IEnumerable<T> and the other for IEnumerable . And, IList<T> doesn't inherit from IList. I don't know why IEnumerable<T> is designed in other way. 回答1: Straight from the horse's mouth (Hejlsberg): Ideally all of the generic collection interfaces (e.g.

Enumerating Collections that are not inherently IEnumerable?

邮差的信 提交于 2019-12-17 06:35:27
问题 When you want to recursively enumerate a hierarchical object, selecting some elements based on some criteria, there are numerous examples of techniques like "flattening" and then filtering using Linq : like those found here : link text But, when you are enumerating something like the Controls collection of a Form, or the Nodes collection of a TreeView, I have been unable to use these types of techniques because they seem to require an argument (to the extension method) which is an IEnumerable

What is the difference between IEnumerator and IEnumerable? [duplicate]

孤街醉人 提交于 2019-12-17 05:35:26
问题 This question already has answers here : Closed 10 years ago . Possible Duplicate: Can anyone explain IEnumerable and IEnumerator to me? What are the differences between IEnumerator and IEnumerable? 回答1: IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface, this in turn allows readonly access to a collection. A collection that implements IEnumerable can be used with a foreach statement. Definition IEnumerable public IEnumerator GetEnumerator

What is the difference between IEnumerator and IEnumerable? [duplicate]

喜欢而已 提交于 2019-12-17 05:35:08
问题 This question already has answers here : Closed 10 years ago . Possible Duplicate: Can anyone explain IEnumerable and IEnumerator to me? What are the differences between IEnumerator and IEnumerable? 回答1: IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface, this in turn allows readonly access to a collection. A collection that implements IEnumerable can be used with a foreach statement. Definition IEnumerable public IEnumerator GetEnumerator

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'xxx'

邮差的信 提交于 2019-12-17 04:02:53
问题 There are a couple of posts about this on Stack Overflow but none with an answer that seem to fix the problem in my current situation. I have a page with a table in it, each row has a number of text fields and a dropdown. All the dropdowns need to use the same SelectList data so I have set it up as follows: Controller ViewData["Submarkets"] = new SelectList(submarketRep.AllOrdered(), "id", "name"); View <%= Html.DropDownList("submarket_0", (SelectList)ViewData["Submarkets"], "(none)") %> I

ReadOnlyCollection or IEnumerable for exposing member collections?

为君一笑 提交于 2019-12-17 02:28:09
问题 Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection? class Bar { private ICollection<Foo> foos; // Which one is to be preferred? public IEnumerable<Foo> Foos { ... } public ReadOnlyCollection<Foo> Foos { ... } } // Calling code: foreach (var f in bar.Foos) DoSomething(f); As I see it IEnumerable is a subset of the interface of ReadOnlyCollection and it does not allow the user to modify

How can I add an item to a IEnumerable<T> collection?

浪尽此生 提交于 2019-12-17 02:19:30
问题 My question as title above. For example, IEnumerable<T> items = new T[]{new T("msg")}; items.ToList().Add(new T("msg2")); but after all it only has 1 item inside. Can we have a method like items.Add(item) ? like the List<T> 回答1: You cannot, because IEnumerable<T> does not necessarily represent a collection to which items can be added. In fact, it does not necessarily represent a collection at all! For example: IEnumerable<string> ReadLines() { string s; do { s = Console.ReadLine(); yield

Shorter syntax for casting from a List<X> to a List<Y>?

穿精又带淫゛_ 提交于 2019-12-17 01:41:12
问题 I know its possible to cast a list of items from one type to another (given that your object has a public static explicit operator method to do the casting) one at a time as follows: List<Y> ListOfY = new List<Y>(); foreach(X x in ListOfX) ListOfY.Add((Y)x); But is it not possible to cast the entire list at one time? For example, ListOfY = (List<Y>)ListOfX; 回答1: If X can really be cast to Y you should be able to use List<Y> listOfY = listOfX.Cast<Y>().ToList(); Some things to be aware of (H/T

Extension Methods for IEnumerable<Enum>?

余生长醉 提交于 2019-12-14 04:25:17
问题 I have a bunch of different enums, such as... public enum MyEnum { [Description("Army of One")] one, [Description("Dynamic Duo")] two, [Description("Three Amigo's")] three, [Description("Fantastic Four")] four, [Description("The Jackson Five")] five } I wrote an extension method for any Enum to get the Description attribute if it has one. Simple enough right... public static string GetDescription(this Enum currentEnum) { var fi = currentEnum.GetType().GetField(currentEnum.ToString()); var da

IEnumerable question: Best performance?

寵の児 提交于 2019-12-14 03:55:46
问题 Quick question: Which one is faster? foreach (Object obj in Collection) { if(obj.Mandatory){ ... } } or foreach (Object obj in Collection.FindAll(o => o.Mandatory)) { ... } and if you know a faster suggestion, i'd be pleased to know. Thank you 回答1: The following test code prints the system ticks (1 tick = 100 nanoseconds) for iterating through 10 million objects. The FindAll is slowest and the for loop is fastest as expected. But the overhead of the iteration is measured in nanoseconds per