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
69
votes
7 answers

Why use .AsEnumerable() rather than casting to IEnumerable?

One of the extension methods on IEnumerable is .AsEnumerable(). This method converts the enumerable object it was called on into an instance of IEnumerable. However, since an object must implement IEnumerable in order to apply to this…
Erik Forbes
  • 35,357
  • 27
  • 98
  • 122
68
votes
10 answers

Why does IEnumerable.ToList() return List instead of IList?

The extension method ToList() returns a List. Following the same pattern, ToDictionary() returns a Dictionary. I am curious why those methods do not type their return values as IList and IDictionary
Waldfee
  • 833
  • 6
  • 10
67
votes
3 answers

What should I use an IEnumerable or IList?

Can anyone tell me when I should use either. For example, I think I should use an IList when I want to access the .Count of the collection or an individual item, correct? Thank you.
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254
63
votes
8 answers

Split an IEnumerable into fixed-sized chunks (return an IEnumerable> where the inner sequences are of fixed length)

I want to take an IEnumerable and split it up into fixed-sized chunks. I have this, but it seems inelegant due to all the list creation/copying: private static IEnumerable> Partition(this IEnumerable items, int…
Alastair Maw
  • 5,373
  • 1
  • 38
  • 50
61
votes
5 answers

filtering a list using LINQ

i have a list of project objects: IEnumerable projects a Project class as a property called Tags. this is a int[] i have a variable called filteredTags which is also a int[]. So lets say my filtered tags variable looks like this: int[]…
leora
  • 188,729
  • 360
  • 878
  • 1,366
61
votes
4 answers

Difference between IEnumerable and IEnumerable?

What is the difference between IEnumerable and IEnumerable? I've seen many framework classes implementing both these interfaces, therefore I would like to know what advantages one get by implementing both? Please have a look how they've been…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
57
votes
15 answers

Pair-wise iteration in C#, or sliding window enumerator

If I have an IEnumerable like: string[] items = new string[] { "a", "b", "c", "d" }; I would like to loop thru all the pairs of consecutive items (sliding window of size 2). Which would be ("a", "b"), ("b", "c"), ("c", "d") My solution was is…
f3lix
  • 29,500
  • 10
  • 66
  • 86
57
votes
8 answers

IEnumerable is empty?

I know it probably doesnt matter/affect performance for the most part but I hate the idea of getting an IEnumerable and doing .Count(). Is there a IsEmpty or NotEmpty or some function? (similar to stl empty())
user34537
56
votes
6 answers

Casting IEnumerable to List

I was wondering if it is possible to cast an IEnumerable to a List. Is there any way to do it other than copying out each item into a list?
RCIX
  • 38,647
  • 50
  • 150
  • 207
56
votes
5 answers

Correct, idiomatic way to use custom editor templates with IEnumerable models in ASP.NET MVC

This question is a follow-up for Why is my DisplayFor not looping through my IEnumerable? A quick refresh. When: the model has a property of type IEnumerable you pass this property to Html.EditorFor() using the overload that only…
GSerg
  • 76,472
  • 17
  • 159
  • 346
55
votes
2 answers

Chart of IEnumerable LINQ equivalents in Scala?

Possible Duplicate: LINQ analogues in Scala I am looking for chart which shows equivalents in Scala of LINQ methods for IEnumerable: First is head Select is map SingleOrDefault is ... (I don't know) ... and so on Does anyone know anything of…
greenoldman
  • 16,895
  • 26
  • 119
  • 185
55
votes
10 answers

Why is .ForEach() on IList and not on IEnumerable?

Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface? I've noticed when writing LINQ-y code that .ForEach() is a nice idiom to use. For example, here is a piece of code that takes the following inputs, and…
Olema
  • 561
  • 1
  • 4
  • 4
55
votes
4 answers

Calculating Count for IEnumerable (Non Generic)

Can anyone help me with a Count extension method for IEnumerable (non generic interface). I know it is not supported in LINQ but how to write it manually?
Homam
  • 23,263
  • 32
  • 111
  • 187
55
votes
9 answers

How to check if a variable is an IEnumerable of some sort

basically I'm building a very generic T4 template and one of the things I need it to do is say print variable.ToString(). However, I want it to evaluate lists and foreach through them and instead print ListItem.ToString() My T4 template does not…
Earlz
  • 62,085
  • 98
  • 303
  • 499
53
votes
2 answers

C# Distinct on IEnumerable with custom IEqualityComparer

Here's what I'm trying to do. I'm querying an XML file using LINQ to XML, which gives me an IEnumerable object, where T is my "Village" class, filled with the results of this query. Some results are duplicated, so I would like to perform a…
Fueled
  • 8,776
  • 9
  • 29
  • 31