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
207
votes
23 answers

How to check if IEnumerable is null or empty?

I love string.IsNullOrEmpty method. I'd love to have something that would allow the same functionality for IEnumerable. Is there such? Maybe some collection helper class? The reason I am asking is that in if statements the code looks cluttered if…
Schultz9999
  • 8,717
  • 8
  • 48
  • 87
200
votes
6 answers

Return all enumerables with yield return at once; without looping through

I have the following function to get validation errors for a card. My question relates to dealing with GetErrors. Both methods have the same return type IEnumerable. private static IEnumerable GetErrors(Card card) { var…
John Oxley
  • 14,698
  • 18
  • 53
  • 78
177
votes
6 answers

IEnumerable to string

I've never stumbled across this before, but I have now and am surprised that I can't find a really easy way to convert an IEnumerable to a string. The best way I can think of is string str = new string(myEnumerable.ToArray());, but, to me, it…
Connell
  • 13,925
  • 11
  • 59
  • 92
175
votes
12 answers

How to get the index of an element in an IEnumerable?

I wrote this: public static class EnumerableExtensions { public static int IndexOf(this IEnumerable obj, T value) { return obj .Select((a, i) => (a.Equals(value)) ? i : -1) .Max(); } public…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
166
votes
3 answers

ICollection Vs List in Entity Framework

I only watched a few webcasts before I went head first in to designing a few Entity Framework applications. I really didn't read that much documentation and I feel like I am suffering for it now. I have been using List in my classes, and it has…
wil
  • 2,019
  • 2
  • 15
  • 14
158
votes
6 answers

What's the difference between IQueryable and IEnumerable

I'm confused as to the difference. Being fairly new to .Net, I know I can query IEnumerables using the Linq extensions. So what is this IQueryable and how does it differ? See also What is the difference between IQueryable[T] and IEnumerable[T]?…
James Hay
  • 12,580
  • 8
  • 44
  • 67
150
votes
2 answers

IList vs IEnumerable for Collections on Entities

When I have entities in my domain with lists of things, should they be exposed as ILists or IEnumerables? E.g. Order has a bunch of OrderLines.
pondermatic
  • 6,453
  • 10
  • 48
  • 63
140
votes
8 answers

Check if one IEnumerable contains all elements of another IEnumerable

What is the fastest way to determine if one IEnumerable contains all the elements of another IEnumerable when comparing a field/property of each element in both collections? public class Item { public string Value; public Item(string…
Brandon Zacharie
  • 2,320
  • 2
  • 24
  • 29
139
votes
5 answers

ReadOnlyCollection or IEnumerable for exposing member collections?

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 foos; // Which one is to be preferred? …
Erik Öjebo
  • 10,821
  • 4
  • 54
  • 75
131
votes
6 answers

The order of elements in Dictionary

My question is about enumerating Dictionary elements // Dictionary definition private Dictionary _Dictionary = new Dictionary(); // add values using add _Dictionary.Add("orange", "1"); _Dictionary.Add("apple",…
Captain Comic
  • 15,744
  • 43
  • 110
  • 148
131
votes
5 answers

What is the purpose of AsQueryable()?

Is the purpose of AsQueryable() just so you can pass around an IEnumerable to methods that might expect IQueryable, or is there a useful reason to represent IEnumerable as IQueryable? For example, is it supposed to be for cases like…
Ocelot20
  • 10,510
  • 11
  • 55
  • 96
128
votes
6 answers

Convert from List into IEnumerable format

IEnumerable _Book_IE List _Book_List How shall I do in order to convert _Book_List into IEnumerable format?
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
127
votes
15 answers

Should I always return IEnumerable instead of IList?

When I'm writing my DAL or other code that returns a set of items, should I always make my return statement: public IEnumerable GetRecentItems() or public IList GetRecentItems() Currently, in my code I have been trying to use…
KingNestor
  • 65,976
  • 51
  • 121
  • 152
126
votes
4 answers

What is the difference between IEnumerator and IEnumerable?

Possible Duplicate: Can anyone explain IEnumerable and IEnumerator to me? What are the differences between IEnumerator and IEnumerable?
Shaun
120
votes
3 answers

Why does this string extension method not throw an exception?

I've got a C# string extension method that should return an IEnumerable of all the indexes of a substring within a string. It works perfectly for its intended purpose and the expected results are returned (as proven by one of my tests, although…
ArtOfCode
  • 5,702
  • 5
  • 37
  • 56