IEnumerable VS IList

IEnumerable VS IList

29 Mar 2024
Intermediate
34.8K Views
5 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with WebAPI Course

IEnumerable-vs-Ilist: An Overview

In this LINQ Tutorial, you will learn the difference between IEnumerable and Ilist with some actual Programming Examples. In LINQ to query data from collections, we use IEnumerable and IList for data manipulation. ICollection inherits IEnumerable and it is inherited by IList, hence it has all the features of it and except this, it has its own features also as shown in the below figure.

                                                      difference between IEnumerable and Ilist

In the previous article, I also explained the difference between IEnumerable and IQueryable.Now Let’s see IEnumerable and Ilist, their features, and learn how to use both the features to boost your LINQ Query performance. But before the difference between them get to know what is Ilist and what is IEnumerable individually.

What is IList?

IList exists in System.Collections Namespace. IList is used to access an element in a specific position/index in a list. Like IEnumerable, IList is also best for querying data from in-memory collections like List, Array, etc. IList is useful when you want to Add or remove items from the list. IList can find out the no of elements in the collection without iterating the collection. IList supports deferred execution. IList doesn't support further filtering.

IList Example:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace CollectionsDemo

{
    internal class Program

    {
        static void Main(string[] args)

        {

          IList EmployeeName = new List();

          EmployeeName.Add("Rahul");
          EmployeeName.Add("Pragya");
          EmployeeName.Add("Chinmay");
          EmployeeName.Add("Priya");

          foreach (string item in EmployeeName)

          {

              Console.WriteLine(item);

          }

}
    }
}

Output:

Rahul
Pragya
Chinmay
Priya

Explanation:

In this example, we created an IList of strings using the List class. Then we added four strings to the list using the Add method. Basically, IList extends ICollection and adds methods for accessing elements by index. Use it when you need to access elements by index, and you need to modify the collection too.

What is IEnumerable?

IEnumerable exists in System.Collections Namespace.IEnumerable can move forward only over a collection, it can’t move backward and between the items.IEnumerable is best for querying data from in-memory collections like List, Array etc.IEnumerable doesn't support add or removing items from the list.Using IEnumerable we can find out the no of elements in the collection after iterating the collection.IEnumerable supports deferred execution.IEnumerable supports further filtering.

IEnumerable Example:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace HelloWorld
{
	public class Program
	{
		public static void Main(string[] args)
		{
			IEnumerable numbers = new int[] { 1, 2, 3, 4, 5 };

foreach (int number in numbers)

{

Console.WriteLine(number);

}
		}
	}
}

Output:


1
2
3
4
5

Explanation:

IEnumerable is the most basic collection type. IEnumerable represents a sequence of elements that can be enumerated, but it does not provide any methods for modifying the sequence. So Use it when you need to iterate over a sequence of elements, but you don't need to modify the sequence.

IEnumerable-vs-Ilist in a nutshell:

1 .IEnumerable and Ilist, are interfaces and both are part of the System.Collection.
2. IEnumerable is inherited by IList means IList is an extended version of IEnumerable.
3. Both have different execution styles. That means the query will not give results until we enumerate over data.
4. IList is used when we want to manipulate over collection.
5. IEnumerable is only for the Readonly approach.
6. IEnumerable internally uses to IEnumerator interface and has the GetEnumerator method.
7. IEnumerable can not return no of elements without iteration whereas IList has extra methods e.g. add, remove, contains, count(read-only) 8. so it's easy to get no of elements without iteration.
9. Both are good for querying data from in-memory collections like List, Array, etc.
11. IEnumerable has further filter support whereas IList does not.
12. Both execute "select query" on the server side, load data in-memory on the client side, and then filter the data. so beneficial for LINQ to 13.Object and LINQ to XML queries.

Summary:

In this article, I tried to explain the difference between IEnumerable and ILIst. I hope after reading this article you will be able to boost your LINQ query performance. I would like to have feedback from my blog readers. Please post your feedback, questions, or comments about this article. Enjoy coding..!

FAQs

IList is a combination of IEnumerable and IQueryable with the additional feature to add/delete items from the list. IList gives full control over stored data. Unlike IEnumerable and IQueryable, we can add or delete items from the list. Also, we can get index-based data from the list.

The main difference is that with a List<N> we can add and remove items, while with an IEnumerable<N> we can only iterate over the items. We can't add or remove items

ICollection offers all the functionalities of the IEnumerable and adds a few new functionalities to it. ICollection allows to addition or remove of elements in the collection which is not possible with IEnumerable.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this