.NET ListT Concat vs AddRange
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
.NET provides robust support for collections using `List`````<T>``````, a part of the `System.Collections.Generic` namespace. Two commonly used methods for manipulating lists are `Concat` and `AddRange`. Both methods extend a list but behave differently regarding execution, performance, and underlying mechanics. This article explores these differences through technical explanations and examples, allowing developers to determine which method best suits their requirements.
Understanding List`````<T>````` Basics
`List`````<T>`````` is a generic collection allowing developers to create strongly-typed lists with predetermined types at compile-time. Some key features of `List`````<T>`````` include:
- Efficient random access owing to its array-based storage.
- Dynamic resizing, although this can impact performance due to the cost of resizing operations.
- Support for LINQ queries, enhancing the expressiveness of data operations.
Given these characteristics, it's critical to understand how `Concat` and `AddRange` influence the behavior and efficiency of `List`````<T>`````` manipulations.
Concat Method
Overview
`Concat` is a LINQ extension method found in `System.Linq`. It enables developers to concatenate two sequences, returning a new sequence without modifying the original lists.
Technical Details
- Non-destructive: `Concat` creates an enumerator over the second sequence and appends it to the first without merging data into a new collection. The original `List`````<T>`````` objects remain unchanged.
- Deferred Execution: This method uses deferred execution, meaning the concatenation occurs when the returned IEnumerable is enumerated.
- Returns IEnumerable: Operations with `Concat` result in lazy evaluation, producing an `IEnumerable`````<T>``````.
Example
- Destructive: This method modifies the original `List`````<T>``````, making it longer by adding the elements of the specified collection at its end.
- Immediate Execution: Unlike `Concat`, `AddRange` executes immediately and commits changes to the list's state.
- Does not return a new object: Instead, it extends the existing list.
- Concat is ideal when immutability is essential, or when working with deferred execution patterns in LINQ.
- AddRange is better suited when a mutable operation is necessary and immediate execution with a single, extended list is preferred.
Related reading
- .NET Out Of Memory Exception - Used 1.3GB but have 16GB installed
- Netty and Project Loom
- Neural Network Diverging instead of converging
- Neural Network Mini Batch Gradient Descent
- .NET obfuscation tools/strategy
- .Net regex what is the word character w?
- Neural network weighting
- NFA minimization without determinization

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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.