Adding Elements to a List in C# - Add, Insert, AddRange with Examples

๐Ÿ‘๏ธ 54 Views
|
๐Ÿ“… Jul 29, 2026
|
โฑ๏ธ 11 min read
Adding Elements to a List in C# - Add, Insert, AddRange with Examples

Working with lists and collections is something you do in almost every C# project โ€” storing user data, managing product inventories, handling API responses, building game objects. The List<T> class in C# is the go-to collection for most of these tasks, and knowing all the ways to add, insert, and manage elements in it will save you time every single day.

In this guide we will cover everything about adding elements to a List in C# โ€” from basic Add() and Insert() to adding ranges, adding at the beginning, removing elements, and the common mistakes that trip up beginners.

List<T> vs Array in C#

Before we dive in, it is worth understanding why we use List<T> instead of a plain array in most situations.

  • Arrays have a fixed size โ€” once you declare string[] arr = new string[5], it can only ever hold 5 elements. You cannot add or remove elements dynamically.
  • List<T> is dynamic โ€” it grows and shrinks automatically as you add and remove elements. Under the hood it uses an array that it resizes when needed, but you never have to manage that yourself.

Use an array when you know the exact number of elements upfront and it will never change. Use List<T> for everything else โ€” which is most real-world situations.

Creating a List in C#

First, make sure you have the right namespace imported at the top of your file:

using System;
using System.Collections.Generic;

There are several ways to create a List depending on your situation:

// Empty list โ€” grows as you add items
List<string> myList = new List<string>();

// List with initial capacity hint (performance optimisation)
// Reserves memory for 10 items upfront but list is still empty
List<string> myList = new List<string>(10);

// List pre-filled with values (collection initialiser)
List<string> myList = new List<string>() { "Lorem", "Ipsum", "Dolor" };

// Using var (type is inferred automatically)
var myList = new List<string>();

// C# 12+ โ€” even cleaner syntax
List<string> myList = ["Lorem", "Ipsum", "Dolor"];

About the capacity parameter: new List<string>(10) does NOT create a list with 10 empty slots. It reserves memory for 10 items to avoid frequent resizing as you add elements โ€” but the list is still empty. myList.Count will return 0.

Adding Elements With Add()

The Add() method appends a single element to the end of the list. This is the most commonly used method and what you will reach for in most situations:

List<string> myList = new List<string>();

myList.Add("Lorem");
myList.Add("Ipsum");
myList.Add("Dolor");

// Print all items
foreach (string item in myList) {
    Console.WriteLine(item);
}

// Output:
// Lorem
// Ipsum
// Dolor

Each call to Add() places the new item after all existing items. The order is preserved exactly as you added them. myList.Count increases by 1 with every Add call.

Adding Elements at a Specific Index With Insert()

The Insert() method lets you add an element at any position in the list. All existing elements at that index and beyond are shifted one position to the right to make room:

List<string> myList = new List<string>() { "Lorem", "Ipsum", "Dolor" };

// List before insert: ["Lorem", "Ipsum", "Dolor"]
//                      index 0   index 1   index 2

myList.Insert(1, "Hello");
// List after insert:  ["Lorem", "Hello", "Ipsum", "Dolor"]
//                      index 0   index 1  index 2  index 3

myList.Insert(3, "World");
// List after insert:  ["Lorem", "Hello", "Ipsum", "World", "Dolor"]
//                      index 0   index 1  index 2  index 3  index 4

foreach (string item in myList) {
    Console.WriteLine(item);
}

// Output:
// Lorem
// Hello
// Ipsum
// World
// Dolor

Bug in the original post: The output showed "Worl" instead of "World" โ€” that was a typo. The correct output is "World". Also the original output was missing "Dolor" which shifted to the last position after the inserts.

Adding Multiple Elements at Once With AddRange()

Instead of calling Add() multiple times in a loop, you can add a whole collection at once using AddRange():

List<string> myList = new List<string>() { "Lorem", "Ipsum" };

// Add multiple items at once from an array
string[] newItems = { "Dolor", "Sit", "Amet" };
myList.AddRange(newItems);

// Or add from another list
List<string> anotherList = new List<string>() { "Hello", "World" };
myList.AddRange(anotherList);

foreach (string item in myList) {
    Console.Write(item + " ");
}
// Output: Lorem Ipsum Dolor Sit Amet Hello World

Inserting a Range at a Specific Index With InsertRange()

Just like Insert() but for multiple elements at once:

List<string> myList = new List<string>() { "First", "Last" };

string[] middle = { "Second", "Third", "Fourth" };
myList.InsertRange(1, middle); // Insert starting at index 1

foreach (string item in myList) {
    Console.WriteLine(item);
}

// Output:
// First
// Second
// Third
// Fourth
// Last

Adding Elements to the Beginning of the List

There is no built-in AddFirst() method in List โ€” but you can use Insert(0, item) to add to the beginning:

List<string> myList = new List<string>() { "B", "C", "D" };

// Add to the beginning
myList.Insert(0, "A");

foreach (string item in myList) {
    Console.Write(item + " ");
}
// Output: A B C D

If you frequently need to add to the beginning of a collection, consider using LinkedList<T> instead of List<T> โ€” it has an AddFirst() method and does not need to shift all elements to make room.

Working With Different Data Types

List<T> works with any data type โ€” not just strings. The T is a generic type parameter that you replace with whatever type you need:

// List of integers
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);

// List of doubles
List<double> prices = new List<double>() { 9.99, 14.99, 4.50 };

// List of booleans
List<bool> flags = new List<bool>() { true, false, true };

// List of custom objects
public class Student {
    public string Name { get; set; }
    public int Age { get; set; }
}

List<Student> students = new List<Student>();
students.Add(new Student { Name = "Randhir", Age = 22 });
students.Add(new Student { Name = "Priya",   Age = 20 });

foreach (Student s in students) {
    Console.WriteLine(s.Name + " โ€” Age: " + s.Age);
}
// Output:
// Randhir โ€” Age: 22
// Priya โ€” Age: 20

Useful List Methods You Should Know

Beyond Add and Insert, here are the List methods you will use most often in real projects:

List<string> myList = new List<string>() { "A", "B", "C", "D", "E" };

// Count โ€” number of items
Console.WriteLine(myList.Count);           // 5

// Contains โ€” check if item exists
Console.WriteLine(myList.Contains("C"));  // True

// IndexOf โ€” find position of an item
Console.WriteLine(myList.IndexOf("D"));   // 3

// Remove โ€” remove by value (first match)
myList.Remove("B");
// List: ["A", "C", "D", "E"]

// RemoveAt โ€” remove by index
myList.RemoveAt(0);
// List: ["C", "D", "E"]

// Clear โ€” remove all elements
myList.Clear();
// List: []

// Sort โ€” sort alphabetically / numerically
List<int> nums = new List<int>() { 5, 2, 8, 1, 9 };
nums.Sort();
// nums: [1, 2, 5, 8, 9]

// Reverse โ€” reverse the order
nums.Reverse();
// nums: [9, 8, 5, 2, 1]

// ToArray โ€” convert list to array
string[] arr = myList.ToArray();

Common Mistakes to Avoid

1. ArgumentOutOfRangeException on Insert

List<string> myList = new List<string>() { "A", "B" };

// โŒ Wrong โ€” index 5 does not exist, list only has 2 items
myList.Insert(5, "X"); // ArgumentOutOfRangeException

// โœ… Correct โ€” valid indices for Insert are 0 to Count (inclusive)
myList.Insert(2, "X"); // adds at the end โ€” same as Add()

2. Confusing Count with Index

List<string> myList = new List<string>() { "A", "B", "C" };

// myList.Count = 3
// Valid indices = 0, 1, 2 (always Count - 1 for the last item)

// โŒ Wrong โ€” index 3 does not exist
Console.WriteLine(myList[3]); // ArgumentOutOfRangeException

// โœ… Correct โ€” last item is at index Count - 1
Console.WriteLine(myList[myList.Count - 1]); // C

// Or use Last() from LINQ
using System.Linq;
Console.WriteLine(myList.Last()); // C

3. Modifying a List While Iterating

// โŒ Wrong โ€” modifying a list inside foreach throws InvalidOperationException
foreach (string item in myList) {
    if (item == "B") {
        myList.Remove(item); // InvalidOperationException!
    }
}

// โœ… Correct โ€” iterate backwards with a for loop when removing
for (int i = myList.Count - 1; i >= 0; i--) {
    if (myList[i] == "B") {
        myList.RemoveAt(i);
    }
}

// Or use RemoveAll (cleanest approach)
myList.RemoveAll(item => item == "B");

Quick Reference โ€” All Add Methods

Method What It Does Example
Add() Adds one item to the end list.Add("item")
Insert() Adds one item at a specific index list.Insert(1, "item")
AddRange() Adds multiple items to the end list.AddRange(array)
InsertRange() Adds multiple items at a specific index list.InsertRange(1, array)
Insert(0) Adds one item to the beginning list.Insert(0, "item")

Final Thought

The List<T> class is one of the most used data structures in C# development. Once you are comfortable with Add(), Insert(), AddRange(), and the common pitfalls, you will handle collections confidently in any C# project โ€” from simple console apps to large ASP.NET applications.

The most important habit to build early is checking your index bounds before inserting or accessing elements. An ArgumentOutOfRangeException is one of the most common runtime errors in C# and it is always avoidable with a simple bounds check.

Working on a C# project and have a specific list or collection question? Reach out via our contact page โ€” happy to help.

Subscribe to Our Newsletter

Join Our Developer Community!

Unsubscribe Anytime | No Spam