How to concatenate two IEnumerableT into a new IEnumerableT?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- How to configure log4net programmatically from scratch no config
- How to connect ASP.Net Core to a SQL Server Docker container on Mac
- How to consolidate date ranges in a list in C
- How to consume a Scoped service from a Singleton?
- How to Consume from specific TopicPartitionOffset with Confluent.Kafka in .Net
- How to convert a column of DataTable to a List
- How to convert a list of generic tasks of different types that are stored in a ListTask, to a TaskListobject?
- How to convert DateTime? to DateTime

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.