In .NET, collections are very important for handling and working with groups of objects. C# provides different interfaces and classes like IEnumerable, ICollection, IList, and List. Each of these has its own use and features. Thus, knowing the difference between them is important for writing better programs.

1. IEnumerable
The IEnumerable interface is the most basic of the collection interfaces. It enables iteration over a collection of a specified type using a foreach loop.
Key Points
- It is defined in the System. Collections namespace.
- Provides one method: GetEnumerator.
- Read-only: You cannot add, remove, or modify items.
- Suitable for forward-only iteration.
Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
IEnumerable<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
int index = 0;
foreach (var num in numbers)
{
Console.WriteLine($"Element at index {index}: {num}"); // Outputs: 1 2 3 4 5
index++;
}
}
}
Output

2. ICollection
ICollection builds upon IEnumerable by adding methods for modifying the collection, such as adding or removing items.
Key Points
- Defined in the System.Collections.Generic namespace.
- Provides methods like Add, Remove, and Clear.
- Includes properties like Count to get the number of elements.
Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
ICollection<string> fruits = new List<string> { "Apple", "Banana" };
fruits.Add("Cherry");
fruits.Remove("Banana");
foreach (var fruit in fruits)
{
Console.WriteLine(fruit); // Outputs: Apple Cherry
}
Console.WriteLine($"Total fruits: {fruits.Count}"); // Outputs: 2
}
}
Output

3. IList
IList extends ICollection by adding support for accessing elements by index, making it ideal for scenarios requiring random access.
Key Points
- Defined in the System.Collections.Generic namespace.
- Allows indexed access (list[0]).
- Supports inserting and removing items at specific indices.
Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
IList<int> numbers = new List<int> { 10, 20, 30 };
// Display initial list
Console.WriteLine("Initial list: " + string.Join(", ", numbers));
// Update value at index 1
numbers[1] = 25;
Console.WriteLine("Updated index 1 to 25: " + string.Join(", ", numbers));
// Insert 35 at index 2
numbers.Insert(2, 35);
Console.WriteLine("Inserted 35 at index 2: " + string.Join(", ", numbers));
// Remove the first element (index 0)
numbers.RemoveAt(0);
Console.WriteLine("Removed element at index 0: " + string.Join(", ", numbers));
// Display final list with indexes
Console.WriteLine("\nFinal list with indexes:");
for (int i = 0; i < numbers.Count; i++)
{
Console.WriteLine($"Index {i}: {numbers[i]}");
}
}
}
Output

4. List
The list is a concrete implementation of IList. It provides additional methods and properties for dynamic arrays.


Join the conversation! Your thoughts help the community grow.