C#
.NET
collections
programming
IEnumerable

Practical difference between List and IEnumerable

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When working with collections in C#, choosing the appropriate data structure can significantly impact the performance and readability of your code. Two commonly used interfaces for handling collections are `List` and `IEnumerable`. Understanding the practical differences between them will help you make informed decisions in your software development process.

IEnumerable

`IEnumerable` is a foundational interface found in the System.Collections namespace and further enhanced in System.Linq with `IEnumerable`````<T>``````. It represents a forward-only cursor of elements, permitting iteration over a sequence of a specified type. The primary utility of `IEnumerable` is to provide the `GetEnumerator` method, which returns an enumerator that allows iteration over the collection.

Characteristics of IEnumerable

  • Read-Only: `IEnumerable` represents a read-only sequence of data. It does not allow for modification like adding, removing, or updating elements.
  • Lazy Evaluation: Because it supports deferred execution, `IEnumerable` allows queries to be composed and executed when iterated over, which can improve performance by postponing expensive operations until truly necessary.
  • Efficient in Memory Usage: As it does not store the elements but merely gives a mechanism to iterate over them, `IEnumerable` is more memory-efficient for large sequences of data.

Example Usage

  • Mutable: `List` allows for adding, removing, and updating elements, providing dynamic resizing.
  • Indexed Access: This data structure supports indexed access, allowing for quick look-up and modification via an index.
  • Eager Evaluation: Unlike `IEnumerable`, a `List` stores the collection in memory and fully executes any queries made against it immediately.
  • Performance: While adding and removing elements is efficient, especially when modifications are at the end of the list, operations involving the middle or beginning can be costly due to reallocation of the internal array.
  • Use `IEnumerable` when:
    • You want to define a query expression but defer the run until explicitly iterated.
    • There's no need for modification or random access to elements.
    • Memory management is a priority and you aim to minimize overhead.
  • Use `List` when:
    • You need the flexibility to alter the collection dynamically via adds, removes, or updates.
    • Indexed access to elements is required.
    • The sequence is expected to undergo several operations across the lifespan of the application.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.