How to Add Items to a List in C#
By Tan Lee Published on Mar 05, 2025 197
- List.Add(): Adds a single item to the end of the list.
- List.Insert(index, item): Adds an item at a specific position in the list.
- List initializer syntax: Allows you to add items when you first create the list.
- List.AddRange(): Adds multiple items at once to the list.
Add Items to the End with List.Add()
The simplest way to add an item to a list is using List.Add(). It appends a single item to the end of the list.
var numbers = new List<int>(); numbers.Add(5); numbers.Add(10); numbers.Add(15); Console.WriteLine(string.Join(", ", numbers));
Output:
5, 10, 15
Initialize a List with Items
You can also initialize a list with items in one step using list initializer syntax.
Style 1 (Traditional):
var names = new List<string>() { "Alice", "Bob", "Charlie" }; Console.WriteLine(string.Join(", ", names));
Style 2 (Available in .NET 8+):
List<string> names = ["Alice", "Bob", "Charlie"]; Console.WriteLine(string.Join(", ", names));
Output:
Alice, Bob, Charlie
This approach is convenient and concise, especially when you know all the items beforehand.
Insert Items at a Specific Position with List.Insert()
To add an item at a specific index in the list, use List.Insert().
For example, to add an item at the beginning of the list, you can specify index 0:
var letters = new List<string>() { "B", "C", "D" }; letters.Insert(0, "A"); Console.WriteLine(string.Join(", ", letters));
Output:
A, B, C, D
Note: List.Insert() is slower than List.Add() if you’re adding to the beginning of the list frequently, as it needs to shift all other elements. If performance is an issue, consider using a LinkedList, which is optimized for such operations.
Add Multiple Items at Once with List.AddRange()
If you want to append multiple items at once, you can use List.AddRange().
Here’s an example where we add items from one list to another:
var firstList = new List<int>() { 1, 2, 3 }; var secondList = new List<int>() { 4, 5, 6 }; firstList.AddRange(secondList); Console.WriteLine(string.Join(", ", firstList));
Output:
1, 2, 3, 4, 5, 6
This method is great for combining lists or adding a batch of items from any IEnumerable source (like arrays, lists, etc.).
Each of these methods serves a different purpose depending on whether you need to append a single item, insert at a specific position, initialize the list, or add multiple items.
- Primitive types in C#
- How to set permissions for a directory in C#
- How to Convert Int to Byte Array in C#
- How to Convert string list to int list in C#
- How to convert timestamp to date in C#
- How to Get all files in a folder in C#
- How to use Channel as an async queue in C#
- Case sensitivity in JSON deserialization