DEV Community

Fabrizio Bagalà
Fabrizio Bagalà

Posted on • Edited on

IEnumerable, ICollection, IList in .NET

ℹ️ Information
The code in the following article was tested with .NET 5, 6 and 7.

.NET Framework offers a wide range of interfaces for handling collections of data. Three of the most significant ones are IEnumerable, ICollection, and IList. These interfaces play a key role in the correct use of data collections and in efficient resource management.

IEnumerable

IEnumerable is the most basic interface for managing collections. Its primary task is to provide an enumerator to traverse a collection. The enumerator is an object that implements the IEnumerator interface, which allows you to access all elements of a collection, one at a time.

using System.Linq;

IEnumerable<int> numbers = Enumerable.Range(1, 5);
foreach (var number in numbers)
{
    Console.WriteLine(number);
}
Enter fullscreen mode Exit fullscreen mode

It has only one method, GetEnumerator(), which returns an enumerator that iterates through the collection.

ICollection

ICollection extends IEnumerable and introduces additional functions for managing collections. It includes methods to add and remove elements, and properties to determine the number of elements present in the collection. ICollection also introduces the concept of "read-only", allowing you to create collections that cannot be modified.

using System.Collections.Generic;
using System.Linq;

ICollection<int> numbers = Enumerable.Range(1, 5).ToList();
numbers.Add(6);
numbers.Remove(1);
Console.WriteLine(numbers.Count);
Enter fullscreen mode Exit fullscreen mode

Methods:

  • Add(), adds an item to the collection.
  • Clear(), removes all items from the collection.
  • Contains(), determines whether the collection contains a specific value.
  • Remove(), removes the first occurrence of a specific object from the collection.
  • CopyTo(), copies the elements of the collection into an array, starting with a particular index of the array.

Properties:

  • Count, gets the number of elements contained in the collection.
  • IsReadOnly, gets a value indicating whether the collection is read-only.

IList

IList extends ICollection and adds features for managing indexed collections, like arrays and lists. IList allows you to access the elements of a collection through an index, and provides methods to insert and remove elements at specific positions. Moreover, IList allows you to determine the position of a specific element within the collection.

using System.Collections.Generic;
using System.Linq;

IList<int> numbers = Enumerable.Range(1, 5).ToList();
numbers.Insert(0, 0);
numbers.RemoveAt(1);
Console.WriteLine(numbers[0]);
Enter fullscreen mode Exit fullscreen mode

Methods:

  • IndexOf, determines the index of a specific item in the collection.
  • Insert, inserts an item to the collection at the specified index.
  • RemoveAt, removes the collection item at the specified index.

It adds only one property, Item[Int32], which provides access to a collection item through an index.

Comparison

All these interfaces provide an increasing level of functionality. The IEnumerable interface is the most generic and provides the ability to iterate through a collection. The ICollection interface extends IEnumerable by adding methods to modify the collection and properties to obtain information about the collection. Finally, the IList interface extends ICollection and adds specific features for indexed collections.

The choice of which interface to use depends on specific needs. If you only need to iterate over a collection, IEnumerable is enough. If you need to modify the collection, then ICollection is the interface to use. Finally, if you are working with indexed collections, the IList interface is the right choice.

Conclusion

The interfaces IEnumerable, ICollection, and IList are fundamental for manipulating collections of data. Each interface introduces a series of methods that provide an increasing set of functionalities, from simple iteration of elements to advanced management of indexed collections. A deep understanding of these interfaces and their methods is crucial for any .NET developer.

References

Top comments (0)