Does a ListT guarantee that items will be returned in the order they were added?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In .NET, List<T> preserves element order during normal iteration, and that order initially matches insertion order. Confusion usually appears when code later sorts, inserts at specific indexes, or mutates the list concurrently. The key is understanding what operations preserve sequence and what operations intentionally change it.
Basic Ordering Behavior of List<T>
List<T> is a dynamic array. Each Add appends at the end, so iteration reflects insertion order.
Output order is first, second, third.
Capacity Growth Does Not Reorder Items
As List<T> grows, internal capacity can expand. This reallocates the internal array but does not change logical order.
Capacity is an implementation detail; index ordering remains stable unless you run reordering operations.
Operations That Change Sequence
List<T> preserves current order, but some methods intentionally alter it.
Examples:
- '
Sort' - '
Reverse' - '
Insertat lower index' - '
RemoveAtwhich shifts following items left'
After these operations, enumeration still follows list order, but the order is no longer original insertion order.
Comparison With Other Collections
If order matters, choosing the right collection is critical.
- '
List<T>preserves order and supports indexing.' - '
HashSet<T>does not promise insertion order.' - '
Queue<T>preserves FIFO order with enqueue and dequeue semantics.' - '
LinkedList<T>preserves sequence but has different performance tradeoffs.'
Use List<T> when random index access and stable sequence are both important.
Concurrency and Order Assumptions
List<T> is not thread-safe for concurrent writes. If multiple threads add or remove without synchronization, observed results can become unpredictable or throw exceptions.
Ordering guarantees assume correct synchronization in multithreaded code.
API Contract Considerations
If your method returns IEnumerable<T> backed by a list, callers often assume ordering is meaningful. Document ordering semantics explicitly.
If order matters to consumers, encode that guarantee in tests and documentation.
Serialization Behavior
JSON serializers preserve list element order. If order is significant in API payloads, list-based models are suitable. Be careful when converting to set or dictionary structures in the pipeline, because those types may not preserve the same sequence semantics.
Performance Notes
List<T> provides:
- fast append in amortized constant time
- fast index access in constant time
- slower inserts and removals near front due to element shifts
If your workload requires frequent front insertions, evaluate alternatives like LinkedList<T> or deque-style data structures.
Common Pitfalls
A common pitfall is assuming insertion order survives calls to Sort or Reverse. Another is relying on list order while mutating from multiple threads without locks. Teams sometimes replace List<T> with hash-based collections and unknowingly lose sequence guarantees. API methods also return ordered data without documenting that order, causing fragile consumer assumptions. Finally, front-heavy insert patterns can create performance issues when List<T> is used blindly.
Summary
- '
List<T>preserves current sequence order during iteration.' - Initial order is insertion order when using append operations.
- Internal resizing does not change logical order.
- Sorting, reversing, and indexed inserts can change sequence.
- Concurrency requires synchronization to keep behavior predictable.
- Document and test ordering guarantees when APIs depend on them.

