IEnumerable :- IEnumerable is an Interface presents in
System.Collections namespace and it is used for collection of items
to be loop through each items one at a time.
In simple words we can say that the collections which implemets the
IEnumerable Interface can be loop through Foreach loop.
IEnumerable is a Read Only Interface it means if you want to add or remove the items from IEnumerable you
can not do that.
It has enumrated data means it gets data items from Interface one by one.
If you have a situation where you do not want to add and modify the collection items and You need to
use items one by one then IEnumerable is the best to use.
Due to deffered execution it is much faster than list because in deffered execution it only gets data
when it needed.
List :- List is a Collection Class presents in
System.Collections namespace and It is collection of Items and we can access
those items using the indexes. List is mainly used where you want to frequently add or remove the items
from collection.
In simple words we can say that List is like a dynamic array that is shrink and grows when we remove or add the items into it. We can direct access any items from List using the index of the List.
Below is the Tabular Representation of the Differences in IEnumerable and List.
Sr.No | IEnumerable | List |
---|---|---|
1 | IEnumerable is an Interface and present in System.Collections Namespace. | List is a Class and present in System.Collections Namespace. |
2 | IEnumerable is Read Only means you can not Add or Remove the items. | List is not Read Only means you Add or Remove the items. |
3 | IEnumerable supports the deferred execution means items fetched only when they needed during iterations. | List do not supports the deferred execution means it creates a new List imediately in memory when it is called. |
4 | IEnumerable is used where u only need to enumerate data once. | List is used where u need to enumerate data multiple times. |
5 | Data gets one by one using Interface in in-memory . | Data is in-memory so faster for frequently used data. |
6 | It has better memory utilization for large set of data. | It has memory overhead in case of large set of data. |
7 | Direct Indexing is not supported by it. | It has full support for Direct Indexing. |
IEnumerable Example
internal class Program
{
static void Main(string[] args)
{
IEnumerable enumItems = new List() {11,34,67,86,39,71,55,89,98,104 };
Console.WriteLine("Items of enumItems:-");
foreach (int item in enumItems)
{
Console.Write(item+ " ");
}
int maxValueFromItems=enumItems.Max();
Console.WriteLine();
Console.WriteLine("Maximum values for an Item is :-"+maxValueFromItems);
Console.ReadKey();
}
}
Output of the above program
Items of enumItems:- 11 34 67 86 39 71 55 89 98 104
Maximum values for an Item is :-104
List Example
internal class Program
{
static void Main(string[] args)
{
List listItems = new List() {"I","Love","India","And","Indians" };
Console.WriteLine("display of Items in List :-");
foreach (var item in listItems)
{
Console.Write(item+ " ");
}
int listCount= listItems.Count;
Console.WriteLine();
Console.WriteLine("Number of Items in List :-"+ listCount);
Console.ReadKey();
}
}
Output of the above program
display of Items in List :-
I Love India And Indians
Number of Items in List :-5
About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the
applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server,
EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.