Lists in C#: A Guide to Dynamic Data Storage

Introduction
In this article, we will discuss Lists in C#, a powerful and flexible data structure that allows you to store and manipulate collections of data. We will explore how to create, add, remove, and search items in a List, as well as some useful methods and properties available to manipulate Lists. Finally, we will provide examples to help you better understand how Lists work in C#.
What are Lists in C#?
Lists in C# are part of the System.Collections.Generic namespace and are used to store and manage a collection of objects. They are dynamic in size, allowing you to add or remove elements at runtime, and can store items of any data type, including custom types.
Declaring and Initializing Lists
There are two primary ways to declare and initialize a List in C#:
Using the List Constructor
You can create a new List by using the List constructor and specifying the data type of the elements
it will store. For example:
Loading code snippet...
This creates a new List called numbers
that will store integers.
Using Collection Initializers
You can also initialize a List with a collection initializer, which allows you to specify the initial elements within the declaration:
Loading code snippet...
This creates a new List called fruits
that stores strings, and initializes it with three elements.
Adding Items to a List
There are two primary methods to add items to a List:
Add() Method
The Add()
method is used to add a single item to the end of the List:
Loading code snippet...
AddRange() Method
The AddRange()
method is used to add a collection of items to the end of the List:
Loading code snippet...
Accessing Elements of a List
There are two primary ways to access elements in a List:
Using Indexers
You can access individual elements of a List using indexers, similar to arrays:
Loading code snippet...
ForEach Loop
You can also iterate through all the elements of a List using a foreach
loop:
Loading code snippet...
Removing Items from a List
There are several methods to remove items from a List:
Remove() Method
The Remove()
method removes the first occurrence of a specified item:
Loading code snippet...
RemoveAt() Method
The RemoveAt()
method removes the item at a specified index:
Loading code snippet...
RemoveRange() Method
The RemoveRange()
method removes a range of elements starting from a specified index:
Loading code snippet...
Sorting a List
There are two primary ways to sort a List in C#:
Using Sort() Method
The Sort()
method sorts the elements of the List using the default comparer for the data type:
Loading code snippet...
Using Comparison Delegate
You can also sort a List using a custom comparison delegate. This allows you to define your own sorting criteria:
Loading code snippet...
Searching in a List
There are two primary methods for searching elements in a List:
Find() Method
The Find()
method searches for an element that matches a specified predicate:
Loading code snippet...
FindAll() Method
The FindAll()
method returns a new List containing all elements that match a specified predicate:
Loading code snippet...
List Capacity and Count
The Count
property returns the number of elements in the List, while the Capacity
property returns the current capacity of the List. The capacity is automatically increased as elements are added to the List.
Loading code snippet...
Converting Lists to Arrays
You can convert a List to an array using the ToArray()
method:
Loading code snippet...

C# Array vs List
May 2, 2022Working with ReadOnlyList
A ReadOnlyList is a wrapper around a List that provides a read-only view of the underlying List. This can be useful when you want to expose a List to external code without allowing modification:
Loading code snippet...
Examples
Let’s look at a couple of examples to demonstrate the use of Lists in C#.
Example 1: Basic List Operations
Loading code snippet...
In this example, we create a List of strings, add elements to it, and then remove an element. The output will be:
Loading code snippet...
Example 2: Sorting, Searching and Filtering
Loading code snippet...
In this example, we create a List of integers, sort the elements, search for the first even number, and filter the list to get all even numbers. The output will be:
Loading code snippet...
Conclusion
In this article, we discussed Lists in C#, a versatile data structure that allows you to store and manipulate collections of data. We explored various methods to create, add, remove, sort, and search elements in a List, as well as working with ReadOnlyList and converting Lists to arrays. The provided examples should help you better understand how to work with Lists in C#.
FAQs
- What is the main difference between a List and an array in C#?The main difference is that a List is dynamic in size, allowing you to add or remove elements at runtime, while an array has a fixed size.
- How can I remove all elements from a List?You can use the
Clear()
method to remove all elements from a List:fruits.Clear();
- Can a List store items of different data types?A List can only store items of a single data type. However, you can create a List of type
object
to store items of different types, but you will need to cast them to their appropriate types when accessing them. - How do I check if a List contains a specific item?You can use the
Contains()
method to check if a List contains a specific item:bool exists = fruits.Contains("Banana");
- Can I create a List of custom objects?Yes, you can create a List of custom objects. Just specify the custom object type when declaring the List:
List<MyCustomClass> customList = new List<MyCustomClass>();