What is IEnumerable interface in C #? What if we don't use it?

Searched the internet What is an IEnumerable interface in C #? The problem he is solving? What if we don't use it? But it didn't work out. Many posts explain how to implement it.

I 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 outputs:

Sourav

Sachin

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

I would appreciate if any of you can help me understand this and what is the point of using IEnumerable with an example? What if we don't use it?

+3


source to share


3 answers


Apart from reading the documentation , I would describe IEnumerable<T>

as a set of Ts, it can be iterated over with and many other functions can be performed (e.g. Where (), Any () and Count ()), however it is not designed to add and remove items. What a List<T>

.

This is useful because it is a fundamental interface for many collections, different levels of data access and ORM use it, and many extension methods are automatically included for it.

Many concrete implementations of lists, arrays, sums, queues, stacks implement it, allowing a wide variety of collections to use its extension methods.

Also, collections that implement IEnumerable or IEnumerable can be used in a loop foreach

.

From msdn



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

In your example code, you have a named variable that will be IEnumerable<string>

, it is important to understand what that will be IEnumerable<string>

, whether you are using the keyword var

or not. var

allows you to avoid writing the type so explicitly every time.

TL; DR

It is a common base interface for many different collection types, which allows your collection to be used in foreach loops and provides many additional extension methods for free.

+6


source


IEnumerable

and much more preferable IEnumerable<T>

is the standard way of processing the "sequence of elements" pattern.

The idea of ​​each type : IEnumerable<T>

looks like if there is a label: "ENUMERATE ME". No matter what's in there: order item queue, collection of controls, records from sql query, trays of xml items, etc. Etc. Etc. - it's all the same from an enumerable point of view: you have a sequence, and you can do something for each of the sequence.



Note that it is IEnumerable

somewhat limited: no counter, no indexed access, no guarantee for repeatable results, no way to check if an enumerated value is empty, but to get a counter and check if there is anything. The simplicity allows it to cover almost all use cases, from collections to special sequences (custom iterators, linq queries, etc.).

The question has been asked several times, here are some answers: 1 , 2 , 3

+1


source


MSDN

"The drawback of excluding IEnumerable and IEnumerator is that the collection class no longer interacts with the foreach statement or equivalent statements in other common language languages ."

So, you need to implement this interface so that your custom collection type can be used with other CLR languages. This is similar to the CLS requirement.

0


source







All Articles