ienumerable

yield return vs. return IEnumerable<T>

只谈情不闲聊 提交于 2019-12-21 07:15:32
问题 I've noticed something curious about reading from an IDataReader within a using statement that I can't comprehend. Though I'm sure the answer is simple. Why is it that whilst inside the using (SqlDataReader rd) { ... } if I directly perform a yield return the reader stays open for the duration of the read. But if I perform a direct return calling a SqlDataReader extension method (outlined below) that the reader closes before the enumerable can be actualized? public static IEnumerable<T>

Is IEnumerable.Any faster than a for loop with a break?

谁都会走 提交于 2019-12-21 05:06:09
问题 We experienced some slowness in our code opening a form and it was possibly due to a for loop with a break that was taking a long time to execute. I switched this to an IEnumerable.Any() and saw the form open very quickly. I am now trying to figure out if making this change alone increased performance or if it was accessing the ProductIDs property more efficiently. Should this implementation be faster, and if so, why? Original Implementation: public bool ContainsProduct(int productID) { bool

How to get all descriptions of enum values with reflection?

醉酒当歌 提交于 2019-12-21 05:00:47
问题 So I need to get a List<string> from my enum Here is what I have done so far: enum definition [Flags] public enum ContractorType { [Description("Recipient")] RECIPIENT = 1, [Description("Deliver")] DELIVER = 2, [Description("Recipient / Deliver")] RECIPIENT_DELIVER = 4 } HelperClass with method to do what I need: public static class EnumUtils { public static IEnumerable<string> GetDescrptions(Type enumerator) { FieldInfo[] fi = enumerator.GetFields(); List<DescriptionAttribute> attributes =

List<T> and IEnumerable difference

蹲街弑〆低调 提交于 2019-12-21 04:58:13
问题 While implementing this generic merge sort, as a kind of Code Kata, I stumbled on a difference between IEnumerable and List that I need help to figure out. Here's the MergeSort public class MergeSort<T> { public IEnumerable<T> Sort(IEnumerable<T> arr) { if (arr.Count() <= 1) return arr; int middle = arr.Count() / 2; var left = arr.Take(middle).ToList(); var right = arr.Skip(middle).ToList(); return Merge(Sort(left), Sort(right)); } private static IEnumerable<T> Merge(IEnumerable<T> left,

Is it possible to clone an IEnumerable<T> instance, saving a copy of the iteration state?

梦想与她 提交于 2019-12-21 03:56:08
问题 I'd like to create a copy of an IEnumerator<T> so that I can restart the enumeration process from a particular location in the collection. Clearly, there is no benefit to doing so for collections that implement IList , since we can remember the index of interest. Is there a clever way to accomplish this task using a combination of yield statements and Linq functions? I could not find a suitable Clone() method to copy the enumerator, and would like to avoid using Enumerable.Skip() to

How to make IEnumerable<T> readonly?

∥☆過路亽.° 提交于 2019-12-20 19:57:24
问题 Why are the lists list1Instance and p in the Main method of the below code pointing to the same collection? class Person { public string FirstName = string.Empty; public string LastName = string.Empty; public Person(string firstName, string lastName) { this.FirstName = firstName; this.LastName = lastName; } } class List1 { public List<Person> l1 = new List<Person>(); public List1() { l1.Add(new Person("f1","l1")); l1.Add(new Person("f2", "l2")); l1.Add(new Person("f3", "l3")); l1.Add(new

Why is Enumerator.MoveNext not working as I expect it when used with using and async-await?

℡╲_俬逩灬. 提交于 2019-12-20 11:21:53
问题 I would like to enumerate through a List<int> and call a async method. If I do this in this way: public async Task NotWorking() { var list = new List<int> {1, 2, 3}; using (var enumerator = list.GetEnumerator()) { Trace.WriteLine(enumerator.MoveNext()); Trace.WriteLine(enumerator.Current); await Task.Delay(100); } } the result is: True 0 but I expect it to be: True 1 If i remove the using or the await Task.Delay(100) : public void Working1() { var list = new List<int> {1, 2, 3}; using (var

How to append enumerable collection to an existing list in C#

梦想与她 提交于 2019-12-20 10:25:33
问题 i have three functions which are returning an IEnumerable collection. now i want to combine all these into one List. so, is there any method by which i can append items from IEnumerable to a list. i mean without for each loop? 回答1: Well, something will have to loop... but in LINQ you could easily use the Concat and ToList extension methods: var bigList = list1.Concat(list2).Concat(list3).ToList(); Note that this will create a new list rather than appending items to an existing list. If you

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<>

不想你离开。 提交于 2019-12-20 10:14:45
问题 Is there any specific reason why indexing is not allowed in IEnumerable. I found a workaround for the problem I had, but just curious to know why it does not allow indexing. Thanks, 回答1: Because it's not. Indexing is covered by IList . IEnumerable means "I have some of the powers of IList, but not all of them." Some collections (like a linked list), cannot be indexed in a practical way. But they can be accessed item-by-item. IEnumerable is intended for collections like that. Note that a

Yield keyword value added?

非 Y 不嫁゛ 提交于 2019-12-20 10:11:20
问题 still trying to find where i would use the "yield" keyword in a real situation. I see this thread on the subject What is the yield keyword used for in C#? but in the accepted answer, they have this as an example where someone is iterating around Integers() public IEnumerable<int> Integers() { yield return 1; yield return 2; yield return 4; yield return 8; yield return 16; yield return 16777216; } but why not just use list<int> here instead. seems more straightforward.. 回答1: If you build and