programming
C#
data structures
IList
performance analysis

Is returning IListT worse than returning T or ListT?

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

Choosing collection return types in C# APIs is a design decision, not just syntax preference. Returning IList<T>, List<T>, or T[] communicates different guarantees about mutability, performance, and caller expectations. The best choice depends on your contract, not on a blanket rule.

Start from API Contract, Not Implementation

A public method should expose only what callers need. If consumers only need enumeration, return IEnumerable<T> or IReadOnlyList<T>. If consumers must edit elements by index, IList<T> may be appropriate.

Key idea:

  • return the narrowest type that matches required operations,
  • keep implementation type private so you can change internals later.

Example:

csharp
1public interface IUserRepository
2{
3    IReadOnlyList<User> GetActiveUsers();
4}

This contract allows internal use of List<User>, arrays, or other storage without breaking callers.

IList<T> vs List<T> vs T[]

Returning List<T>

Returning List<T> exposes concrete behavior and mutable methods such as Add or Remove. That can be useful for internal APIs but often too permissive for public contracts.

csharp
1public List<int> GetScores()
2{
3    return new List<int> { 10, 20, 30 };
4}

Risk: callers may mutate returned data and assume ownership semantics you did not intend.

Returning IList<T>

IList<T> hides concrete type but still exposes mutability operations. It offers flexibility of implementation while signaling index-based mutable access.

csharp
1public IList<int> GetScores()
2{
3    return new List<int> { 10, 20, 30 };
4}

This is not inherently slower. Interface dispatch overhead is usually negligible compared with allocation and data processing costs.

Returning T[]

Arrays provide compact memory and fast indexed access. They are fixed-size but still mutable element-by-element.

csharp
1public int[] GetScores()
2{
3    return new[] { 10, 20, 30 };
4}

Arrays work well when size is stable and interop or serialization expects array shapes.

When IList<T> Is a Good Choice

Use IList<T> when all of these are true:

  • callers need index access,
  • callers need mutation,
  • you want to hide concrete collection type.

If callers should not mutate, IList<T> is usually too broad. Prefer IReadOnlyList<T> instead.

csharp
1public IReadOnlyList<int> GetScoresReadOnly()
2{
3    return new List<int> { 10, 20, 30 };
4}

This creates a clearer contract and prevents accidental writes through API surface.

Performance Reality

Most performance questions here are dominated by copying and allocation, not by choosing interface versus concrete return type.

Potential costs to consider:

  • returning a defensive copy each call,
  • converting between list and array frequently,
  • repeated enumeration over deferred queries.

Example of avoidable copying:

csharp
1public IReadOnlyList<int> GetScores()
2{
3    // Expensive if called often on large collections
4    return _scores.ToList();
5}

If data is immutable for caller purposes, return cached read-only view once.

csharp
1private readonly List<int> _scores = new() { 10, 20, 30 };
2private IReadOnlyList<int>? _cachedView;
3
4public IReadOnlyList<int> GetScores()
5{
6    _cachedView ??= _scores.AsReadOnly();
7    return _cachedView;
8}

Practical API Patterns

For library and service boundaries:

  • read-only indexed result: IReadOnlyList<T>
  • streaming query result: IEnumerable<T>
  • mutation required by caller: IList<T> or custom command methods
  • high-performance fixed-size results: T[]

For internal private methods, returning List<T> can be fine if it keeps implementation simple.

Example balanced approach:

csharp
1public sealed class ProductService
2{
3    private readonly List<Product> _cache = new();
4
5    public IReadOnlyList<Product> GetProducts() => _cache;
6
7    public void AddProduct(Product product) => _cache.Add(product);
8}

Callers can read products but must use explicit service operations for mutation.

Binary Compatibility and Future Changes

Returning interface types can improve API longevity. If you return List<T> today and later want a different structure, callers may depend on list-specific methods and break.

Returning IReadOnlyList<T> or IEnumerable<T> keeps migration paths open.

This matters in shared packages where breaking API changes carry rollout cost.

Common Pitfalls

A common pitfall is returning IList<T> while expecting consumers not to mutate. The type itself permits mutation, so callers are not wrong to use it.

Another issue is defensive copying on every call for fear of mutation. This can create heavy allocation pressure in hot paths.

Teams also conflate interface return types with slower execution. In most business code, the overhead difference is insignificant.

Finally, exposing List<T> from public APIs leaks implementation details and reduces future flexibility.

Summary

  • IList<T> is not inherently worse than List<T> or T[].
  • Choose return types based on contract: mutability, indexing, and abstraction needs.
  • Prefer IReadOnlyList<T> when callers need indexing without mutation.
  • Performance problems usually come from copying and allocation, not interface dispatch.
  • Keep public APIs narrow so implementation details can evolve safely.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms