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
86
votes
2 answers

Practical difference between List and IEnumerable

By reading similar posts I've learned that a List is a type of IEnumerable. But I'm really wondering what the practical difference between those two actually is. To someone who always have used a List and never used IEnumerable: What is the…
Robin
  • 1,927
  • 3
  • 18
  • 27
86
votes
9 answers

Remove an item from an IEnumerable collection

I have a popuplated IEnumerable collection. I want to remove an item from it, how can I do this? foreach(var u in users) { if(u.userId = 1123) { // remove! } } I know your not suppose to remove while looping, so I don't mind either…
loyalflow
  • 14,275
  • 27
  • 107
  • 168
83
votes
10 answers

Why doesn't Any() work on a c# null object

When calling Any() on a null object, it throws an ArgumentNullException in C#. If the object is null, there definitely aren't 'any', and it should probably return false. Why does C# behave this way?
DefenestrationDay
  • 3,712
  • 2
  • 33
  • 61
80
votes
1 answer

Do I need to consider disposing of any IEnumerable I use?

It's recently been pointed out to me that various Linq extension methods (such as Where, Select, etc) return an IEnumerable that also happens to be IDisposable. The following evaluates to True new int[2] {0,1}.Select(x => x*2) is IDisposable Do…
Rob
  • 4,327
  • 6
  • 29
  • 55
79
votes
8 answers

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityComparer here) where an IComparer or an IEqualityComparer was expected. I can't…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
77
votes
4 answers

Convert DataRowCollection to IEnumerable

I would like to do something like this in .NET 3.5. What's the quickest way? IEnumerable collection = TypedDataSet.TypedTableBase.Rows as IEnumerable;
Abdu
  • 16,129
  • 13
  • 59
  • 84
77
votes
3 answers

Difference between IEnumerable Count() and Length

What are the key differences between IEnumerable Count() and Length?
balalakshmi
  • 3,968
  • 6
  • 40
  • 48
76
votes
12 answers

Does C# have IsNullOrEmpty for List/IEnumerable?

I know generally empty List is more prefer than NULL. But I am going to return NULL, for mainly two reasons I have to check and handle null values explicitly, avoiding bugs and attacks. It is easy to perform ?? operation afterwards to get a return…
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
76
votes
3 answers

Params IEnumerable c#

Why cant I use an IEnumerable with params? Will this ever be fixed? I really wish they would rewrite the old libraries to use generics...
Dested
  • 6,294
  • 12
  • 51
  • 73
73
votes
7 answers

generic NOT constraint where T : !IEnumerable

As per the title, is it possible to declare type-negating constraints in c# 4 ? The use-case of interest was to allow the following overloads to co-exist void doIt(T what){} void doIt(IEnumerable whats){} At the moment there…
Cel
  • 6,467
  • 8
  • 75
  • 110
73
votes
3 answers

Differences between IQueryable, List, IEnumerator?

I am wondering what the difference between IQueryable, List, IEnumerator is and when I should use each one? For instance when using Linq to SQL I would do something like this: public List GetUsers() { return db.User.where(/* some query here…
chobo2
  • 83,322
  • 195
  • 530
  • 832
73
votes
5 answers

What is IEnumerable in .NET?

What is IEnumerable in .NET? What is the explanation?
qnatestu1
  • 739
  • 1
  • 5
  • 3
72
votes
10 answers

Convert IEnumerable to DataTable

Is there a nice way to convert an IEnumerable to a DataTable? I could use reflection to get the properties and the values, but that seems a bit inefficient, is there something build-in? (I know the examples like:…
Yvo
  • 18,681
  • 11
  • 71
  • 90
71
votes
43 answers

What's your favorite LINQ to Objects operator which is not built-in?

With extension methods, we can write handy LINQ operators which solve generic problems. I want to hear which methods or overloads you are missing in the System.Linq namespace and how you implemented them. Clean and elegant implementations, maybe…
Nappy
  • 3,016
  • 27
  • 39