what is IEnumerable in .net

what is IEnumerable in .net?

It's ... something... that you can loop over. That might be a List or an Array or (almost) anything else that supports a foreach loop. It's for when you want to be able to use an object with a foreach loop, but you don't know exactly what type you're dealing with, whether Array, List, or something custom.

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

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

Another feature is that this evaluation is lazy, so if you're doing complicated work to evaluate the enumerable as you read from it, that work doesn't happen until it's asked for. This is extra beneficial, because often (say, for searches again) you'll find you might not need to do the work at all.

You can think of IEnumerable as if it were a just-in-time List.

IEnumerable Interface (System.Collections), It's something that you can loop over. That might be a List or an Array or ( almost) anything else that supports a foreach loop. It's for when� When you implement IEnumerable<T>, you must also implement IEnumerator<T> or, for C# only, you can use the yield keyword. Implementing IEnumerator<T> also requires IDisposable to be implemented, which you will see in this example.

It's an interface implemented by Collection types in .NET that provide the Iterator pattern. There also the generic version which is IEnumerable<T>.

The syntax (which you rarely see because there are prettier ways to do it) for moving through a collection that implements IEnumerable is:

IEnumerator enumerator = collection.GetEnumerator();

    object obj = enumerator.Current;
    // work with the object

Which is functionaly equivalent to:

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

If the collection supports indexers, you could also iterate over it with the classic for loop method but the Iterator pattern provides some nice extras like the ability to add synchronization for threading.

IEnumerable<T> Interface, IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all� IEnumerable interface contains the System.Collections.Generic namespace. IEnumerable interface is a generic interface which allows looping over generic or non-generic lists. IEnumerable interface also works with linq query expression. IEnumerable interface Returns an enumerator that iterates through the collection.

First it is an interface. The definition according to MSDN is

Exposes the enumerator, which supports a simple iteration over a non-generic collection.

Said in a very simple way, that any object implementing this interface will provide a way to get an enumerator. An enumerator is used with the 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>();



    // HERE is where the Enumerator is gotten from the List<string> object
    foreach(string dinosaur in dinosaurs)

    // You could do a 
    for(int i = 0; i < dinosaurs.Count; i++)
        string dinosaur = dinosaurs[i];


The foreach looks cleaner.

C# IEnumerable Examples, NET base class library. There is a non-generic IEnumerable interface and there is a generic type-safe IEnumerable<T> interface. Let us first see the definition of the IEnumerable which is given below. As we see from the above image, IEnumerable is an interface that is available in System.Collection namespace. The IEnumerable interface is a type of iteration design pattern. It means we can iterate on the collection of the type IEnumerable.

The short answer is that it's anything you can use a foreach on.

what is IEnumerable in .net, All LINQ methods are extension methods to the IEnumerable<T> interface. That means that you can Most C# collections and all C# arrays implement IEnumerable<T> . Here are some examples: Creating Web API in ASP.NET Core 2.0. IEnumerable in C# In c#, IEnumerable is an interface and it is useful to enable an iteration over non-generic collections and it is available with System.Collections namespace. Generally, the IEnumerable interface will represent an object that can be enumerated and it’s a base interface for all non-generic collections that can be enumerated.

What does the interface IEnumerable do in C#?, NET are implementations of the iterator pattern. So, let's see how these interfaces work, and how to implement them in our List class here. Here we use IEnumerable as the return value of a query expression. We use it with foreach-loops, and also call extension methods on it. Part 1: An IEnumerable is returned from a query expression. A query that selects ints will be of type IEnumerable<int>. Part 2: On an IEnumerable variable, we can use the foreach-loop. This loop iterates with

When To Use IEnumerable, ICollection, IList And List, IEnumerable in c# with examples. In c# ienumerable is an interface and it is useful to enable an iteration over a non-generic collection. IEnumerable is an interface defining a single method GetEnumerator () that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement. It has a single method −

IEnumerable<T> - Using C# LINQ - A Practical Overview, IEnumerable, ICollection and IList are interfaces in the.Net Framework used the most often. IEnumerable is the base of the ICollection and IList interfaces (and many other). All these interfaces provide various functionalities and are useful in various cases.

IEnumerable and IEnumerator in C#: The Differences, IEnumerable is best to query data from in-memory collections like List, Array, etc. While query data from a database, IEnumerable execute a select query on the server side, load data in-memory on a client-side and then filter data. IEnumerable is suitable for LINQ to Object and LINQ to XML queries. IEnumerable supports deferred execution.

  • To be honest, the definition on MSDN is very scary for a beginner, when all you want to do is a foreach.
  • Whilst I can understand why it was closed, I came to this from Google after MSDN wasn't that clear, and the answers have really helped me. If I could, I would vote for re open....This comes high up when Googling for "What is IEnumerable" and I am sure others have found it helpful.
  • The key of IEnumarable is that the backing collection is not enumerated until specifically expanded, be it a foreach or accessor via an indexer. Unfortunately IEnumarable seems to be blindly used as the lowest interface which now causes problems with asynchronous data manipulation (e.g. executing SQL on GarbageCollected contexts). I would say that ICollection is every thing you said apart from the "lazy evaluation" and we should be using that, especially if the data has already been enumerated once!
  • Best simple explanation in plain English, hats-off!
  • I think IEnumerable is poorly named. Mathematically, and in computing, an enumerable is anything that maps to a countable set. So outside of .net an IObservable may be enumerable. In .NET IEnumerable is considered the dual opposite of IObservable in a pull vs push duality. The intention of IEnumerable seems to be what Java calls Iterable, which in my mind is a better name.
  • Note, depending the syntax shown here is the C# 5 equivalent of a foreach. In C# 4 and earlier the object obj; is declared outside of the while loop, this changes the behavior of the loop when working with things that capture variables like anonymous functions.
  • tech-over-tech answer just brings up more confusion and frustration.
  • You can foreach over anything with a GetEnumerator method. It doesn't have to support IEnumerable.
  • yeah, but most of the time IEnumerable <==> foreach
  • Most of your assumptions are characteristics of 'yield return' not specifically IEnumerable. IEnumerable is just interface providing a way for iteration, nothing else.
  • You are using yield return when using IEnumerable, therefore they are completely correlated.