C#
List\`\`\`\`\`<T>\`\`\`\`\`
insertion order
data structures
programming concepts

Does ListT guarantee insertion order?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In the world of programming and software development, understanding the properties of data structures is crucial for writing efficient and correct code. One common question when working with the `List`````<T>`````` class in C# is whether it guarantees insertion order. This involves understanding how `List`````<T>`````` is implemented and what implicit and explicit guarantees it offers concerning the order of elements as they are added.

What is `List`````<T>``````?

`List`````<T>`````` is a part of the .NET Framework's collection classes and is defined in the `System.Collections.Generic` namespace. It represents a strongly typed list of objects that can be accessed by index, with various methods to search, sort, and manipulate lists.

Does `List`````<T>`````` Maintain Insertion Order?

Yes, the `List`````<T>`````` class in C# guarantees the order of insertion. When you add elements to a `List`````<T>``````, they are stored in the order in which they were added. Here's a simple example to demonstrate this property:

  • Array-based Storage: `List`````<T>`````` is essentially a dynamic array. Internally, it uses an array to store its elements. The fundamental characteristic of an array is that it stores items in contiguous memory locations and in the order they are added.
  • Index Management: Elements in a `List`````<T>`````` are accessed via indices that start from zero. When an element is added, it's appended to the end of the internal array unless you specify otherwise with certain list methods (e.g., `Insert` which allows insertion at a specific index).
  • Dynamic Resizing: `List`````<T>`````` can grow dynamically as elements are added. When the current array is full, it's automatically resized (typically by doubling its capacity), and existing elements are copied to this new array, again preserving the order.
  • Add: Appends an element at the end, maintaining the order.
  • Insert(index, item): Inserts an element at a specific position; shifts the rest forward.
  • Remove(item) / RemoveAt(index): Removes an item; relative order of the remaining elements is preserved.
  • Sort: Alters the order by arranging elements based on a sort criteria, which does affect the original insertion order.
  • Guaranteed Order: The contiguous nature of array storage and index-based access inherently maintains the order of elements.
  • Performance: Since `List`````<T>`````` is array-backed, random access (i.e., accessing an element by an index) is O(1)O(1), while adding or removing elements involves O(n)O(n) in the worst-case scenario (e.g., when resizing is required or when removing an item not at the end).

Related reading
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
Track 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.

Practice algorithms

All Rights Reserved.