What is IEnumerable in .net

What is IEnumerable in .net?

+58
ienumerable
Jun 10 '10 at 13:23
source share
5 answers

This is ... something ... that you can go over. It could be List or Array or (almost) everything that supports the foreach . This is for when you want to use an object with a foreach , but you don’t know exactly what type you are dealing with, be it an array, a list, or something else.

So, the first advantage: if your methods accept an IEnumerable rather than an array or list, they become more powerful because you can pass more different types of objects to them.

Now, what makes IEnumerable really stand out is the iterator blocks ( yield keyword in C #). Iterator blocks implement an IEnumerable interface such as List or Array, but they are very special because, unlike List or Array, they often only hold state for one item at a time. Therefore, if you want you to iterate over lines in a very large file, you can write an iterator block to handle file input. Then you will never have more than one line of the file in memory at a time, and if you finish the loop earlier (maybe it was a search and you found what you needed), you may not need to read the whole file. Or, if you read the results from a large SQL query, you can limit the use of your memory to one record.

Another feature is that this estimate is lazy, so if you do the hard work of evaluating an enumerable as you read from it, this work is not done until it asks. This is very useful because often (say, to search again) you find that you may not need to do this work at all.

You can think of IEnumerable as being a list on time.

+49
Jun 10 '10 at 13:37
source share

This is the interface implemented by the Collection types in .NET that provide an iterator pattern . There is also a generic version of IEnumerable<T> .

The syntax (which you rarely see because there are more beautiful ways to do this) is to move around a collection that implements IEnumerable:

 IEnumerator enumerator = collection.GetEnumerator(); while(enumerator.MoveNext()) { object obj = enumerator.Current; // work with the object } 

Which is functionally equivalent:

 foreach(object obj in collection) { // work with the object } 

If the collection supports indexes, you can also iterate over it using the classic for loop method, but the Iterator template provides some nice extras, such as the ability to add synchronization for streaming.

+30
Jun 10 '10 at 13:25
source share

First up is the interface. MSDN definition -

Gives a counter that supports simple iteration over an nonequivalent collection.

It is said in a very simple way that any object that implements this interface will provide a way to get an enumerator. The enumerator is used with foreach as one example.

A List implements the IEnumerable interface.

  // This is a collection that eventually we will use an Enumertor to loop through // rather than a typical index number if we used a for loop. List<string> dinosaurs = new List<string>(); dinosaurs.Add("Tyrannosaurus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Mamenchisaurus"); dinosaurs.Add("Deinonychus"); dinosaurs.Add("Compsognathus"); Console.WriteLine(); // HERE is where the Enumerator is gotten from the List<string> object foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } // You could do a for(int i = 0; i < dinosaurs.Count; i++) { string dinosaur = dinosaurs[i]; Console.WriteLine(dinosaur); } 

The preview looks cleaner.

+10
Jun 10 '10 at 13:25
source share

Short answer: all you can use foreach on.

+5
Jun 10 2018-10-10
source share
  using System.Collections; using static System.Console; namespace SimpleIterators { class X { static int i = 0; public int I { get { return i; } } public X() { WriteLine("X "+i++); } } class Program { public static IEnumerable ValueI(X[] tabl) { for (int i = 0; i < 10; i++) yield return tabl[i].I; } public static IEnumerable ListClass(X[] tabl) { for (int i = 0; i < 10; i++) yield return tabl[i] = new X(); } static void Main(string[] args) { X[] tabl = new X[10]; foreach (X x in ListClass(tabl)); foreach (int i in ValueI(tabl)) WriteLine("X " + i); WriteLine("X " + tabl[0].I); ReadKey(); } } } 
0
Oct. 14 '16 at 11:49
source share



All Articles