Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is IEnumerable interface in c#? What if we dont use it?

Tags:

c#

ienumerable

Searched in internet for What is IEnumerable interface in C#? The problem it solves? What if we don't use it? But never really did not get much. Lots of posts explain how to implement it.

I've also found the following example

List<string> List = new List<string>();
        List.Add("Sourav");
        List.Add("Ram");
        List.Add("Sachin");

       
        IEnumerable names = from n in List where (n.StartsWith("S")) select n;
        // var names = from n in List where (n.StartsWith("S")) select n;

        foreach (string name in names)
        {
            Console.WriteLine(name);
        }

The above ex outputs:

Sourav

Sachin

I wanted to know, the advantage of using IEnumerable in the above example? I can achieve the same using 'var' (commented line).

I would appreciate if anyone of you can help me out to understand this and whats the benefit of using IEnumerable with an example? What if we don't use it?.

like image 687
Maltesh Avatar asked Dec 26 '22 00:12

Maltesh


2 Answers

Beyond reading the documentation I'd describe IEnumerable<T> as a collection of Ts, it can be iterated over and many other functions can be carried out (such as Where(), Any() and Count()) however it's not designed for adding and removing elements. That's a List<T>.

It's useful because it's a fundamental interface for many collections, various data access layers and ORMs use it and many extension methods are automatically included for it.

Many concrete implementations of Lists, Arrays, Bags, Queues, Stacks all implement it allowing a wide variety of collections to use it's extension methods.

Also collections implementing either IEnumerable or IEnumerable can be used in a foreach loop.

From msdn

for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface.

In your code example you've got a variable called names which will be an IEnumerable<string>, it's important to understand that it will be an IEnumerable<string> regardless of whether you use the var keyword or not. var just allows you to avoid writing the type so explicitly each time.

TLDR

It's a common base interface for many different types of collections which let you use your collection in foreach loops and provides a lot of extra extension methods for free.

like image 101
Liath Avatar answered Feb 24 '23 19:02

Liath


IEnumerable and much more preferred IEnumerable<T> are the standard way to handle the 'sequence of elements' pattern.

The idea is each type : IEnumerable<T> looks like if there's a label: "ENUMERATE ME". No matter what's there: queue of order items, collection of controls, records from a sql query, xml element subnodes etc etc etc - it's all the same from enumerable's point of view: you've got a sequence and you can do something for each item from the sequence.

Note that IEnumerable is somewhat limited: there's no count, no indexed access, no guarantee for repeatable results, no way to check if enumerable is empty but to get the enumerator and to check if there is anything. The simplicity allows to cover almost all use cases, from collections to ad-hoc sequences (custom iterators, linq queries etc).

The question was asked multiple times, here're some answers: 1, 2, 3

like image 30
Sinix Avatar answered Feb 24 '23 18:02

Sinix