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

.net - dictionary.Keys.Add?

In .Net, IDictionary defines .Keys and .Values properties, each of which is an ICollection<> rather than IEnumerable<>, which seems like it would be a more natural fit to me. Is there any reasonable use case to call .Add or .Remove on .Keys or…
recursive
  • 83,943
  • 34
  • 151
  • 241
2
votes
1 answer

Elegant way of exporting data from IEnumerable object to an Excel datasheet in c#?

My data is held in an IEnumerable object and I want that to be populated into a table in excel. Basically I am able to export data to the Excel sheet but I dont know export a table in a more elegant way. This is what I have: //Populate column…
MTA
  • 739
  • 2
  • 9
  • 29
2
votes
2 answers

Override the default behaviour of GetEnumerator

I have a requirement where I need to know the calling method to the GetEnumerator(). The best way I could think would be possibly overriding the default behaviour to GetEnumerator to one that I create i.e GetEnumerator([CallerMemberName]string…
jclarkson
  • 169
  • 3
  • 11
2
votes
1 answer

C# IEnumerable Interface

I am trying to understand this example from a MSDN article, to my understanding with the IEnumberable interface, we will be able to use Foreach to loop through the class collection, I am confused at the Main method, why don't we just use: foreach…
k80sg
  • 2,443
  • 11
  • 47
  • 84
2
votes
7 answers

enumerate through all IEnumerables

I need to send different IEnumerables to an Printer object. This printer object will then do something to them, inside a foreach loop. class Printer { public Printer(IEnumerable list) { foreach (var enumerable in list) { …
CasperT
  • 3,425
  • 11
  • 41
  • 56
2
votes
3 answers

C#: Where to implement a custom IEnumerator

Say I have a class that implements IEnumerable. It currently uses the yield keyword in the GetEnumerator() method. But now I need to do a bit more, for example I would like to clean up after myself. To do this, unless I have overlooked anything,…
Svish
  • 152,914
  • 173
  • 462
  • 620
2
votes
1 answer

Error returning IEnumerable to MVC controller

Hi I have a business logic layer that returns selectlistitems to a controller, so that will then pass to the view to populate select lists. I have this method that works: public IEnumerable GetDevices { get { …
DavidB
  • 2,566
  • 3
  • 33
  • 63
2
votes
2 answers

LINQ not working on IEnumerable

I'm using Autofac (I've registered the base nuget package in a console app) and want to take a look at a list of registrations. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using…
Daniel Powell
  • 8,143
  • 11
  • 61
  • 108
2
votes
0 answers

Display Template for IEnumerable

I'm trying to use a Display Template for an IEnumerable collection of an interface, and for some reason I cannot get the Display Template to display for the interface. Here's some brief code to show you what I mean. I have…
2
votes
1 answer

Why IList inherits IEnumerable and IEnumerable again

I have taken a look on IList and ICollection on MSDN by chance, and see that the definition of these two interfaces are: public interface ICollection : IEnumerable, IEnumerable public interface IList : ICollection, IEnumerable,…
cuongle
  • 74,024
  • 28
  • 151
  • 206
2
votes
2 answers

C# Silverlight with Entity Framework - Change Return Type On AutoGenerated EntityQuery?

Background Currently I have a C# Silverlight business application which uses RIA Services. The application is hosted in ASP.NET using the ADO.NET Entity Framework and a domain service class to read and write to the SQL Server database. Scenario I…
Goober
  • 13,146
  • 50
  • 126
  • 195
2
votes
4 answers

How to cast to a List>

I was doing this first to return a particular set of nodes under the parent node that had id equal to 1 which worked fantastically. IEnumerable world1 = LevelData.root. Elements("world"). …
Russell Cargill
  • 270
  • 1
  • 6
  • 19
2
votes
2 answers

Is there a way I can combine two separate iterator blocks to one?

I almost know its impossible and meaningless, but just trying to learn.. I have: public IEnumerable> GetMany() { while (someCondition) yield return GetFew(); } static IEnumerable GetFew() { while…
nawfal
  • 70,104
  • 56
  • 326
  • 368
2
votes
1 answer

I want to create an IEnumerable inheriting Sparse Array with arbitrary indexing

I am trying to create: Sparse Array with arbitrary indexing and have have Sparse Array inherit IEnumerable. So far I have the indexing... the IEnumerable part I don't know what to do. class SparseArray : IEnumerable { private…
torrho
  • 1,823
  • 4
  • 16
  • 21
2
votes
5 answers

Why I should not modify a collection when I am iterating on it

I know that in .net collection types (or at least some collection types) do not allow modify the collection when you are iterating on it. For example in the List class exist a code like this: if (this.version != this.list._version) …