C# tutorials > Core C# Fundamentals > Data Structures and Collections > What is the difference between `List<T>`, `ArrayList`, and `LinkedList<T>`?
What is the difference between `List<T>`, `ArrayList`, and `LinkedList<T>`?
This tutorial explores the key differences between `List
Overview of `List`, `ArrayList`, and `LinkedList`
Key Differences Summarized
The main differences between these collections can be summarized in the following table: Feature `List `ArrayList` `LinkedList Type Safety Generic (strongly-typed) Non-generic (object) Generic (strongly-typed) Underlying Implementation Dynamically-sized array Dynamically-sized array Doubly-linked list Insertion/Deletion (Middle) O(n) - relatively slow O(n) - relatively slow O(1) - very fast Random Access (by index) O(1) - very fast O(1) - very fast O(n) - slow Memory Overhead Lower (contiguous memory) Lower (contiguous memory) Higher (pointers for each node) Boxing/Unboxing No boxing/unboxing Boxing/unboxing for value types No boxing/unboxing
Concepts Behind the Snippet
The core concepts behind these data structures are:
Code Example: Demonstrating Usage
This example demonstrates basic operations on each type of collection. Notice how `ArrayList` can store different data types, while `List
csharp
using System; using System.Collections; using System.Collections.Generic;
public class CollectionComparison
{
public static void Main(string[] args)
{
// List<T>
List<int> intList = new List<int>();
intList.Add(10); intList.Add(20); intList.Insert(1, 15);
Console.WriteLine($"List<T> count: {intList.Count}, Element at index 0: {intList[0]}");
// ArrayList
ArrayList arrayList = new ArrayList();
arrayList.Add(100); arrayList.Add("Hello"); arrayList.Insert(1, 150);
Console.WriteLine($"ArrayList count: {arrayList.Count}, Element at index 0: {arrayList[0]}");
// LinkedList<T>
LinkedList<string> linkedList = new LinkedList<string>();
linkedList.AddFirst("First"); linkedList.AddLast("Last"); linkedList.AddAfter(linkedList.First, "Middle");
Console.WriteLine($"LinkedList<T> count: {linkedList.Count}, First element: {linkedList.First.Value}");
}
}
Real-Life Use Case Section
When to Use Them
Memory Footprint
Pros and Cons
`List
`ArrayList`
`LinkedList
Best Practices
Interview Tip
When discussing these data structures in an interview, emphasize your understanding of their underlying implementations, performance characteristics, and appropriate use cases. Be prepared to discuss the trade-offs involved in choosing one data structure over another. Also, be sure to state that you would almost always prefer `List
Alternatives
FAQ
-
When would I ever use `ArrayList`?
In modern C# development, `ArrayList` is rarely the best choice. It might be encountered in older codebases or when interacting with COM objects that require it. However, for new development, generics like `List` provide better type safety and performance. -
Is `List
` always the best choice for a list-like structure?
Not always. If you require frequent insertions and deletions in the middle of the list and don't need fast random access, `LinkedList` may be a better option, despite its higher memory overhead. -
How does `List
` resize its internal array?
When a `List` reaches its capacity, it typically doubles its size by allocating a new array and copying the elements from the old array to the new one. This operation can be relatively expensive, so it's good practice to initialize the `List ` with an appropriate initial capacity if you know the approximate number of elements it will hold.