22
DecIEnumerable VS IList
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.
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-vs-Ilist in a nutshell:
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..!