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
43
votes
5 answers

C#: How can I make an IEnumerable thread safe?

Say I have this simple method: public IEnumerable GetNumbers() { uint n = 0; while(n < 100) yield return n++; } How would you make this thread safe? And by that I mean that you would get that enumerator once, and have multiple…
Svish
  • 152,914
  • 173
  • 462
  • 620
42
votes
8 answers

Resolving IEnumerable with Unity

Can Unity automatically resolve IEnumerable? Let's say I have a class with this constructor: public CoalescingParserSelector(IEnumerable parserBuilders) and I configure individual IParserBuilder instances in the…
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
40
votes
7 answers

Should I return an IEnumerable or IList?

I wish to return an ordered list of items from a method. Should my return type be IEnumerable or IList?
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
40
votes
7 answers

Using IEnumerable without foreach loop

I've gotta be missing something simple here. Take the following code: public IEnumerable getInt(){ for(int i = 0; i < 10; i++){ yield return i; } } I can call this with: foreach (int j in obj.getInt()){ //do something with j } How…
Mark
  • 106,305
  • 20
  • 172
  • 230
40
votes
4 answers

Why does IEnumerable inherit from IEnumerable?

This might be a old question: Why does IEnumerable inherit from IEnumerable? This is how .NET do, but it brings a little trouble. Every time I write a class implements IEumerable, I have to write two GetEnumerator() functions, one for…
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
39
votes
11 answers

Get previous and next item in a IEnumerable using LINQ

I have an IEnumerable of a custom type. (That I've gotten from a SelectMany) I also have an item (myItem) in that IEnumerable that I desire the previous and next item from the IEnumerable. Currently, I'm doing the desired like this: var previousItem…
Bob2Chiv
  • 1,858
  • 2
  • 19
  • 29
38
votes
2 answers

Resharper: Possible Multiple Enumeration of IEnumerable

I'm using the new Resharper version 6. In several places in my code it has underlined some text and warned me that there may be a Possible multiple enumeration of IEnumerable. I understand what this means, and have taken the advice where…
Ross
  • 4,460
  • 2
  • 32
  • 59
38
votes
7 answers

How to "unroll" a "recursive" structure

Not sure how to call it, but say you have a class that looks like this: class Person { public string Name; public IEnumerable Friends; } You then have a person and you want to "unroll" this structure recursively so you end up with a…
Svish
  • 152,914
  • 173
  • 462
  • 620
37
votes
4 answers

C# List<> GroupBy 2 Values

I'm using C# on Framework 3.5. I'm looking to quickly group a Generic List<> by two properties. For the sake of this example lets say I have a List of an Order type with properties of CustomerId, ProductId, and ProductCount. How would I get the sum…
SaaS Developer
  • 9,835
  • 7
  • 34
  • 45
37
votes
3 answers

Is it possible to turn an IEnumerable into an IOrderedEnumerable without using OrderBy?

Say there is an extension method to order an IQueryable based on several types of Sorting (i.e. sorting by various properties) designated by a SortMethod enum. public static IOrderedEnumerable OrderByX(this IQueryable values, …
NominSim
  • 8,447
  • 3
  • 28
  • 38
37
votes
3 answers

Why there is two completely different version of Reverse for List and IEnumerable?

For the List object, we have a method called Reverse(). It reverse the order of the list 'in place', it doesn't return anything. For the IEnumerable object, we have an extension method called Reverse(). It returns another IEnumerable. I need to…
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
37
votes
8 answers

yield return works only for IEnumerable?

Can I use yield return when the return type is an IGrouping or an IDictionary?
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
36
votes
4 answers

How does Assert.AreEqual determine equality between two generic IEnumerables?

I have a unit test to check whether a method returns the correct IEnumerable. The method builds the enumerable using yield return. The class that it is an enumerable of is below: enum TokenType { NUMBER, COMMAND, …
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
36
votes
3 answers

IEnumerable.Contains with predicate

I need just to clarify that given collection contains an element. I can do that via collection.Count(foo => foo.Bar == "Bar") > 0) but it will do the unnecessary job - iterate the whole collection while I need to stop on the first occurrence. But I…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
34
votes
4 answers

What is difference between push based and pull based structures like IEnumerable and IObservable

In every tech talk, or in every blog post I've read about IEnumerable and IObservable I read that, IEnumerable is pull-based structure and IObservable is push-based structure. I've read that with IObservable we have async calls, where nothing is…
Tornike Gomareli
  • 1,564
  • 2
  • 16
  • 28