How to concatenate two IEnumerableT into a new IEnumerableT?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, the standard way to concatenate two IEnumerable<T> sequences is Concat. It creates a new deferred sequence that enumerates the first source and then the second, which is usually exactly what you want unless you need an eager materialized snapshot instead.
Use Concat for the Normal Case
LINQ already provides the standard solution.
This produces a new IEnumerable<int> that yields 1, 2, 3, 4, 5, 6.
The important detail is that Concat is deferred. It does not copy elements immediately. It simply builds a sequence pipeline that will enumerate both sources later.
Know When You Actually Need a Materialized Collection
If you need a concrete list or array, materialize after concatenation.
This is useful when:
- you need random access
- you need a stable snapshot
- the source sequences may change later
Without ToList or ToArray, the combined sequence will keep pulling from the original enumerables whenever it is iterated.
Handle Null Sources Explicitly
Concat throws if either sequence is null, so guard optional inputs when needed.
This pattern is cleaner than scattering null checks through the calling code.
Understand Deferred Execution
Because Concat is deferred, iteration happens later, not when the expression is built.
This prints 1, 2, 99, 3, 4 because the lists were enumerated after the modification. If that is not what you want, materialize immediately with ToList().
Custom Iterator Is Rarely Necessary
You can write your own iterator, but it is usually unnecessary unless you need custom behavior beyond simple concatenation.
This works, but it is effectively what Concat already provides. Prefer the built-in LINQ method unless you need special rules such as filtering, deduplication, or instrumentation.
Common Pitfalls
- Forgetting that
Concatis deferred can make later source mutations appear unexpectedly in the combined sequence. - Calling
Concaton anullsequence throws unless you guard withEnumerable.Empty<T>(). - Assuming
Concatdeduplicates elements is wrong; it preserves order and includes duplicates. - Materializing too early can waste memory when deferred iteration would have been fine.
- Writing a custom concatenation iterator for the simple case usually adds code without adding value.
Summary
- Use
Enumerable.Concatfor the standard way to combine twoIEnumerable<T>sequences. - Materialize with
ToList()orToArray()when you need a concrete snapshot. - Guard optional inputs with
Enumerable.Empty<T>()to avoid null-related errors. - Remember that
Concatpreserves order and does not remove duplicates. - Prefer the built-in LINQ method unless you need custom concatenation behavior.

