Questions tagged [ienumerable]

IEnumerable, and its generic counterpart IEnumerable are .NET interfaces for iterating (or enumerating) through a collection of items.

The IEnumerable interface (in the System.Collections namespace) contains only a single member, the GetEnumerator() method, which returns an IEnumerator.

The generic counterpart to IEnumerable, IEnumerable<T> (added to the System.Collections.Generic namespace in the .NET framework 2.0) also contains only a single member, the GetEnumerator() method; this returns an IEnumerator<T>.

Interaction with the IEnumerable<T> interface is often done through extension methods, for example First(), Last() and Count().

It is possible to obtain a generic IEnumerable<T> from an IEnumerable by calling either of the extension methods Cast<T>(), or OfType<T>(), depending on the case.

3522 questions
25
votes
4 answers

Array.Count() much slower than List.Count()

When using the extension method of IEnumerable Count(), an array is at least two times slower than a list. Function Count() List 2,299 int[] 6,903 From where did the…
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
25
votes
3 answers

LINQ Single() Exception for 0 or multiple items

I have some IEnumberable collection of items. I use .Single() to find a specific object in the collection. I choose to use Single() because there should only ever be one specific item. However, if one doesn't exist, then I need to create it and add…
Justin Self
  • 6,137
  • 3
  • 33
  • 48
24
votes
4 answers

When should I use IEnumerator for looping in c#?

I was wondering if there are any times where it's advantageous to use an IEnumerator over a foreach loop for iterating through a collection? For example, is there any time where it would be better to use either of the following code samples over the…
lomaxx
  • 113,627
  • 57
  • 144
  • 179
24
votes
1 answer

C# yield in nested method

If I step through the following code the call to ReturnOne() is skipped. static IEnumerable OneThroughFive() { ReturnOne(); yield return 2; yield return 3; yield return 4; yield return 5; } static IEnumerator
David
  • 1,052
  • 1
  • 8
  • 19
24
votes
1 answer

Using Seq functions on an IEnumerable

I'm trying to apply Seq functions on an IEnumerable. More specifically, it's System.Windows.Forms.HtmlElementCollection which implements ICollection and IEnumerable. Since an F# seq is an IEnumerable, I thought I could write let foo =…
John Reynolds
  • 4,927
  • 4
  • 34
  • 42
24
votes
2 answers

Debugging an IEnumerable method

I have a method with returns an IEnumerable and I'm trying to debug the code inside that method. Each time I step through the code in Visual Studio during debug, it steps over the method in question. When I place a breakpoint inside the method it…
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
24
votes
4 answers

Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?

Provided items is the result of a LINQ expression: var items = from item in ItemsSource.RetrieveItems() where ... Suppose generation of each item takes some non-negligeble time. Two modes of operation are possible: Using foreach would…
zzandy
  • 2,263
  • 1
  • 23
  • 43
24
votes
3 answers

Is it possible to extend arrays in C#?

I'm used to add methods to external classes like IEnumerable. But can we extend Arrays in C#? I am planning to add a method to arrays that converts it to a IEnumerable even if it is multidimensional. Not related to How to extend arrays in C#
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
23
votes
1 answer

IEnumerable<> vs List<> as a parameter

In general I tend to use IEnumerable<> as the type when I pass in parameters. However according to BenchmarkDotNet: [Benchmark] public void EnumeratingCollectionsBad() { var list = new List(); for (int i = 0; i < 1000; i++) { …
Murdock
  • 4,352
  • 3
  • 34
  • 63
23
votes
7 answers

IEnumerable vs T[]

I just realize that maybe I was mistaken all the time in exposing T[] to my views, instead of IEnumerable. Usually, for this kind of code: foreach (var item in items) {} item should be T[] or IEnumerable? Than, if I need to get the count of…
stacker
  • 14,641
  • 17
  • 46
  • 74
23
votes
3 answers

Finding symmetric difference with LINQ

I have two collections a and b. I would like to compute the set of items in either a or b, but not in both (a logical exclusive or). With LINQ, I can come up with this: IEnumerable Delta(IEnumerable a, IEnumerable b) { return…
Pierre Arnaud
  • 10,212
  • 11
  • 77
  • 108
23
votes
5 answers

Enumerating Collections that are not inherently IEnumerable?

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…
BillW
  • 3,415
  • 4
  • 27
  • 46
23
votes
7 answers

Code for adding to IEnumerable

I have an enumerator like this IEnumerable page; How can I add a page (eg: D:\newfile.txt) to it? I have tried Add, Append, Concat etc But nothing worked for me.
Sudha
  • 2,078
  • 6
  • 28
  • 53
23
votes
7 answers

Caching IEnumerable

public IEnumerable ListModules() { foreach (XElement m in Source.Descendants("Module")) { yield return new ModuleData(m.Element("ModuleID").Value); } } Initially the above code is great since there is no need to…
djskinner
  • 8,035
  • 4
  • 49
  • 72
23
votes
4 answers

How to group items by index? C# LINQ

Suppose I have var input = new int[] { 0, 1, 2, 3, 4, 5 }; How do I get them grouped into pairs? var output = new int[][] { new int[] { 0, 1 }, new int[] { 2, 3 }, new int[] { 4, 5 } }; Preferably using LINQ
Jader Dias
  • 88,211
  • 155
  • 421
  • 625