A collection in C# is any type that holds a group of values. The framework gives you a lot of them, so the hard question is usually which one to pick.
Use List when you want an ordered group you can add to, Dictionary when you look things up by key, HashSet when you only care whether something is present, and an array when the size never changes and you need the memory laid out flat. Past that it is a trade-off between lookup speed, insert speed, memory and whether more than one thread touches it.
The interfaces matter as much as the classes. IEnumerable lets a caller walk your data once, ICollection adds counting and adding, and IList adds access by index. Accept the smallest one that does the job and you keep your options open.
The articles below are grouped by the type you are reaching for.
Choosing a Collection
Start here if you are not sure which type you want.
- HashSet vs SortedSet
- IEnumerable, ICollection, IList and List
- IEnumerable
- DataTable Class
- Immutable Collections
- Dictionary vs Hashtable
- ArrayList vs List
- List Collection
- Generic List and Dictionary
- Queue, Stack and Hashtable
Lists
List is the default collection in C#, and most of these articles are about the operations that are easy to get slightly wrong.
- Concatenate Lists
- SortedList
- ArrayList
- How to Check if Items of a List Exist in Another List
- How to Clone a List
- How to Update an Item in a List
- Remove Duplicates From a List
- How to Remove Elements From a Generic List in Iteration
- How to Compare Two Lists Through One Property
- How to Filter a List Based on Another List
- Different Ways to Create a Generic List of Anonymous Types
- Techniques for Sorting a List
- How to Get The List of Properties
- How to Randomize a List
- Fastest Way to Check if a List is in Order
- How to Implement a LinkedList
Dictionaries and Lookups
Key to value, and the handful of ways to ask a dictionary a question without throwing.
- Lookup
- Dictionary
- How to Return a Default Value From a Dictionary
- How to Detect if a Dictionary Key Exists
- Different Ways to Iterate Through a Dictionary
- Fastest Way to Get a Dictionary Key by Value
- How to Update the Value Stored in a Dictionary
- How to Get an Item by Index From Dictionary
- How to Merge Dictionaries
- How to Compare Two Dictionaries
Sets, Queues and Stacks
The collections you pick when the shape of the access matters more than the order.
Span, Memory and Allocation-Free Work
How to look at a slice of data without copying it, which is where most of the easy performance wins live.
- Memory Optimization With ArrayPool
- Differences Between Span and Memory
- Ranges and Indices
- Collection Expressions
- How to Convert ReadOnlyMemory to a Byte Array
- Using Memory<T> For Efficient Memory Management
- How to Use Span in C# to Improve Application Performance
Arrays
Fixed size, contiguous memory, and still the fastest thing in the box when you know how many items there are.
- Array Slicing
- Arrays, Single and Multi-Dimensional
- Different Ways to Add Values to a C# Array
- How to Convert Stream to Byte Array
- How to Merge Arrays
- How to Declare an Empty Array
- Convert Byte Array to File
- Convert a File to a Byte Array
- Different Ways to Print The Elements of an Array
- How to Populate an Array With the Same Value
- How to Find the Maximum Value of an Array
- Compare Arrays
- How to Copy Array Elements to New Array
- How to Delete Elements from an Array
- How to Efficiently Randomize an Array
- How to Sum Up Elements of an Array
- How to Print a 2D Array to the Console
- Different Ways to Initialize Arrays
- How To Use ArraySegment
- Remove Duplicates From a C# Array
More Collection Techniques
Everything else we have written about working with groups of values.
Where to Go Next
LINQ is how you query what is in these:
Still unsure? Start with List or Dictionary and change it later. Swapping a collection out is a small refactor, and by then you will know more about how the data is used.
