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
28
votes
3 answers

Performance of Skip (and similar functions, like Take)

I just had a look at the source code of the Skip/Take extension methods of the .NET Framework (on the IEnumerable type) and found that the internal implementation is working with the GetEnumerator method: // .NET framework public static…
Bidou
  • 7,378
  • 9
  • 47
  • 70
28
votes
3 answers

Custom Collection using IEnumerable vs ICollection vs IList

I need to design my own custom GenericCollection class. Now i have plenty of options to derive it using IEnumerable, ICollection, and IList, where later offers some added functionalities. I am little confused that if i go with IEnumerable i might…
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
28
votes
6 answers

returning a generic IEnumerable

I want to have a method that returns any collection type - that implements IEnumerable (?) - (so e.g: List, Stack, Queue, ...) Furthermore it should return any collection type, of any datatype. so i want this method to be able to return a…
Thousand
  • 6,562
  • 3
  • 38
  • 46
27
votes
3 answers

How is yield an enumerable?

I was toying around with yield and IEnumerable and I'm now curious why or how the following snippet works: public class FakeList : IEnumerable { private int one; private int two; public IEnumerator GetEnumerator() { …
Mafii
  • 7,227
  • 1
  • 35
  • 55
27
votes
1 answer

How to Sort IEnumerable List?

I have the following list: IEnumerable cars; The Car object has a model and a year. I want to sort this list by model and then year (within model). What is the best way of doing this?
leora
  • 188,729
  • 360
  • 878
  • 1,366
27
votes
5 answers

How to make the class as an IEnumerable in C#?

So I've got a class and a generic List inside of it, but it is private. class Contacts { List contacts; ... } I want to make the class work as this would do: foreach(Contact in contacts) .... ; like this (not working): Contacts…
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
26
votes
5 answers

Why can't "return" and "yield return" be used in the same method?

Why can't we use both return and yield return in the same method? For example, we can have GetIntegers1 and GetIntegers2 below, but not GetIntegers3. public IEnumerable GetIntegers1() { return new[] { 4, 5, 6 }; } public IEnumerable
Setyo N
  • 1,953
  • 2
  • 26
  • 28
26
votes
7 answers

How can I convert a DataTable into a Dynamic object?

How can I convert a DataTable in IEnumerable? For example, I want to convert any DataTable ID | Name DI | emaN --------- or --------- 1 | x 2 | x 2 | y 1 | y In a list of objects // list 1 …
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
26
votes
3 answers

Reading an IEnumerable multiple times

Let's say I have some code: var items = ItemsGetter.GetAllItems().Where(x => x.SomeProperty > 20); int sum1 = items.Sum(x => x.SomeFlag == true); And for example I need some other sum from the items collection later in the code. int sum2 =…
Aleksandr Ivanov
  • 2,778
  • 5
  • 27
  • 35
26
votes
5 answers

Resharper says I shouldn't use List

I have a method: static void FileChangesDetected(List files) I used Visual Studio 2010 and Resharper. Resharper always recommends that I change the List to IEnumerable, and I'm wondering why this is. In the method, I simply do…
Craig
  • 18,074
  • 38
  • 147
  • 248
26
votes
2 answers

Why is the error handling for IEnumerator.Current different from IEnumerator.Current?

I would have thought that executing the following code for an empty collection that implements IEnumerable would throw an exception: var enumerator = collection.GetEnumerator(); enumerator.MoveNext(); var type = enumerator.Current.GetType(); //…
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
26
votes
3 answers

Create IEnumerable.Find()

I'd like to write: IEnumerable cars; cars.Find(car => car.Color == "Blue") Can I accomplish this with extension methods? The following fails because it recursively calls itself rather than calling IList.Find(). public static T Find(this…
Brian Low
  • 11,605
  • 4
  • 58
  • 63
26
votes
2 answers

Deserialize to IEnumerable class using Newtonsoft Json.Net

I have a project that is currently using Json.Net for Json deserialization classes like these: public class Foo { public Guid FooGuid { get; set; } public string Name { get; set; } public List Bars { get; set; } } public class Bar…
redent84
  • 18,901
  • 4
  • 62
  • 85
26
votes
1 answer

Convert CollectionBase to List or data type usable with Linq

I am using Aspose cells to manipulate Excel spreadsheets. One of the types in the API is a collection of Pictures in the spreadsheet, which derives from CollectionBase: see this…
ChrisCa
  • 10,876
  • 22
  • 81
  • 118
25
votes
3 answers

Loop inside a unit test

Can we have a loop inside a unit test? My method returns an IEnumerable, I would like to unit test this logic where the IEnumerable is created. Basically I wanna test if the count of elements in the IEnumerable are as…
Shankar Raju
  • 4,356
  • 6
  • 33
  • 52