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

Why can't I use the enumerator of an array, instead of implementing it myself?

I have some code like this: public class EffectValues : IEnumerable { public object [ ] Values { get; set; } public IEnumerator GetEnumerator ( ) { return this.Values.GetEnumerator ( ); } …
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
22
votes
2 answers

When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes

I'm having a situation here, I need my class to be inherited from List, but when I do this XmlSerializer does not serialize any property or field declared in my class, the following sample demonstrates: public partial class Form1 : Form { …
Esam Bustaty
  • 346
  • 1
  • 4
  • 13
22
votes
5 answers

Can I have a method returning IEnumerator and use it in a foreach loop?

I need to set the height of every textbox on my form, some of which are nested within other controls. I thought I could do something like this: private static IEnumerator FindTextBoxes(Control rootControl) { foreach (Control control in…
littlecharva
  • 263
  • 1
  • 4
  • 5
22
votes
2 answers

Why is Enumerator.MoveNext not working as I expect it when used with using and async-await?

I would like to enumerate through a List and call a async method. If I do this in this way: public async Task NotWorking() { var list = new List {1, 2, 3}; using (var enumerator = list.GetEnumerator()) { …
22
votes
1 answer

IEnumerable vs IReadOnlyList

What is the difference between choosing IEnumerable vs IReadOnlyList as a return parameter type or input parameter type? IEnumerable provides .Count and .ElementAt which is what is exposed by IReadOnlyList
Ramesh
  • 13,043
  • 3
  • 52
  • 88
22
votes
3 answers

List 'Except' comparison - ignore case

I have two lists and I want to compare them and get the differences, while ignoring any case differences. I have used the following code to get the differences between the two lists but it does not ignore case differences. IEnumerable diff =…
john
  • 1,561
  • 3
  • 20
  • 44
22
votes
8 answers

Buffering a LINQ query

FINAL EDIT: I've chosen Timothy's answer but if you want a cuter implementation that leverages the C# yield statement check Eamon's answer: https://stackoverflow.com/a/19825659/145757 By default LINQ queries are lazily streamed. ToArray/ToList give…
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
22
votes
8 answers

Why does foreach fail to find my GetEnumerator extension method?

I'm trying to make some code more readable. For Example foreach(var row in table) {...} rather than foreach(DataRow row in table.Rows) {...}. To do this I created an extension method: namespace System.Data { public static class MyExtensions { …
jshall
  • 323
  • 1
  • 2
  • 6
22
votes
2 answers

How to implement IEnumerable with GetEnumerator()?

I would like my type to implement IEnumerable . I tried to follow C# in a Nutshell, but something went wrong: public class Simulation : IEnumerable { private IEnumerable Events() { yield return "a"; …
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
21
votes
2 answers

yield return vs. return IEnumerable

I've noticed something curious about reading from an IDataReader within a using statement that I can't comprehend. Though I'm sure the answer is simple. Why is it that whilst inside the using (SqlDataReader rd) { ... } if I directly perform a yield…
pim
  • 12,019
  • 6
  • 66
  • 69
21
votes
7 answers

C# Enumerable.Take with default value

What is the best way to get exactly x values from an Enumerable in C#. If i use Enumerable .Take() like this: var myList = Enumerable.Range(0,10); var result = myList.Take(20); The result will only have 10 elements. I want to fill the missing…
HectorLector
  • 1,851
  • 1
  • 23
  • 33
21
votes
6 answers

how do I chunk an enumerable?

I need an elegant method that takes an enumerable and gets the enumerable of enumerables each of the same number of elements in it but the last one: public static IEnumerable> Chunk(this IEnumerable values, Int32…
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
21
votes
4 answers

C# List vs IEnumerable performance question

Hi suppose these 2 methods: private List GetProviderForType(Type type) { List returnValue = new List(); foreach (KeyValuePair provider in…
theSpyCry
  • 12,073
  • 28
  • 96
  • 152
20
votes
3 answers

Even "IsNullOrEmpty" checks give "Possible multiple enumeration of IEnumerable" warnings

There's a question on SO about "possible multiple enumerations" already, but this question is more specific. Please consider the following method, which takes an IEnumerable as input and executes a given method against each of its…
User
  • 3,244
  • 8
  • 27
  • 48
20
votes
6 answers

LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()

I'd like a general solution but as an example, assume i have an IEnumerable, where some can be parsed as integers, and some cannot. var strings = new string[] { "1", "2", "notint", "3" }; Obviously if i did Select(s => int.Parse(s, temp))…
George Duckett
  • 31,770
  • 9
  • 95
  • 162