How to Insert an Item into a C# List

The List class in C# and .NET represents a strongly typed list of objects. List provides functionality to create a collection of objects, find list items, sort list, search list, and manipulate list items. In List, T is the type of objects. The code examples in this article demonstrates how to add items to a List using C#.

C# List class represents a collection of a type in C#. List.Add(), List.AddRange(), List.Insert(), and List.InsertRange() methods are used to add and insert items to a List.

Previous article: Basic Introduction To List In C#

List is a generic class. You must import the following namespace before using the List class. 

using System.Collections.Generic;  

We can add items to a list during the object initialization.

The following code snippet initialize a list of integers with 5 items.

// Create a List of int  
List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25}; 

The following code snippet initialize a list of strings with 3 items.

// Create a List of strings   
List<string> names = new List<string>()  
                    {"Mahesh Chand", "Neel Beniwal", "Chris Love" };  

Now, let’s create a list of objects and add items during the initialization. 

public class Author  
{  
    // Auto-Initialized properties  
    public string Name { get; set; }  
    public string Book { get; set; }  
    public double Price { get; set; }  
    public string AuthorDetails()  
    {  
        return string.Format("{0} is an author of {1}. Price: ${2}",  
            Name, Book, Price);  
    }  
}

The following code snippet initialize a list of objects (Author class) with 3 items. 

// Create a List of objects  
List<Author> authors = new List<Author>  
{  
    new Author { Name = "Mahesh Chand", Book = "Apress", Price = 49.95 },  
    new Author { Name = "Neel Beniwal", Book = "Apress", Price = 19.95 },  
    new Author { Name = "Chris Love", Book = "PakT", Price = 29.95 }  
};

List.Add() and List.AddRange() methods

List.Add() method adds an object to the end of the List<T>. List.AddRange() method adds a collection of objects to the end of the List<T>.

The following code example adds three int objects to the end of the List<int> using Add method. The code also adds a collection of int using List.AddRange() method.

// Create a List of int  
List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};  
// Add more items to List  
numbers.Add(30);  
numbers.Add(35);   
numbers.Add(40);  
  
// Add a Range  
int[] prime = new int[] { 1, 3, 5, 7, 11, 13};  
numbers.AddRange(prime);  
// Read List items  
foreach (int num in numbers)  
{  
    Console.Write($"{num}, ");       
}

The following code example adds three strings to the end of the List<string>. The code also adds a collection of string using List.AddRange() method.  

// Create a List of strings   
List<string> names = new List<string>()  
                    {"Mahesh Chand", "Neel Beniwal", "Chris Love" };  
// Add more items to the list  
names.Add("Author One");  
names.Add("Author Two");   
names.Add("Author Three");  
  
// Add more items  
names.AddRange(new string[] {"Raj Beniwal", "Allen O'neill" });  
  
// Read List items  
foreach (string name in names)  
{  
    Console.Write($"{name}, ");  
}

The following code example adds three Author objects to the end of the List<Author>. The code also adds a collection of Author using List.AddRange() method.

// Create a List of objects  
List<Author> authors = new List<Author>  
{  
    new Author { Name = "Mahesh Chand", Book = "ADO.NET Programming", Price = 49.95 },  
    new Author { Name = "Neel Beniwal", Book = "Jump Ball", Price = 19.95 },  
    new Author { Name = "Chris Love", Book = "Practical PWA", Price = 29.95 }  
};  
// Add more items to the list  
authors.Add(new Author { Name = "Mahesh Chand", Book = "Graphics with GDI+", Price = 49.95 });  
authors.Add(new Author { Name = "Mahesh Chand", Book = "Mastering C#", Price = 54.95 });  
authors.Add(new Author { Name = "Mahesh Chand", Book = "Jumpstart Blockchain", Price = 44.95 });  
  
// Create a new List of Author  
List<Author> duplicateAuthors = new List<Author>  
{  
    new Author { Name = "Mahesh Chand", Book = "ADO.NET Programming", Price = 49.95 },  
    new Author { Name = "Neel Beniwal", Book = "Jump Ball", Price = 19.95 },  
    new Author { Name = "Chris Love", Book = "Practical PWA", Price = 29.95 }  
};  
// Add List using AddRange  
authors.AddRange(duplicateAuthors);  
  
// Read List objects  
foreach (Author author in authors)  
{  
    Console.WriteLine($"Author: {author.Name}:{author.Book}:{author.Price}");  
}

List.Insert() and List.InsertRange() methods

The Insert method of List<T> class inserts an object at a given position. The first parameter of the method is the 0th based index in the List.<T>.

The InsertRange() method can insert a collection at the given position.

The following code example inserts two int at position 2 and a collection at position 3 respectively.

// Insert an item at index = 3  
numbers.Insert(2, 22);  
numbers.Insert(3, 33);  
  
// Insert a collection     
numbers.InsertRange(5, new int[] { 11, 22, 33, 44, 55});  

The following code example inserts a string at position 3 and a collection at position 2 respectively.

// Insert a string   
names.Insert(3, "Mahesh Chand at 4th");  
  
// Insert a collection of strings  
names.InsertRange(2, new string[] {"Mahesh Beniwal", "Chand Beniwal", "Mahesh Chand Beniwal" });

The following code example inserts an object at position 3 and adds a collection at position 2.

// Insert an object  
authors.Insert(3, new Author { Name = "Mahesh Beniwal", Book = "New Graphics with GDI+", Price = 49.95 } );  
  
// Collection of Author  
List<Author> newAuthors = new List<Author>  
{  
    new Author { Name = "Mahesh Chand", Book = "Database Programming", Price = 49.95 },  
    new Author { Name = "Mahesh Chand Beniwal", Book = "C# Development", Price = 19.95 },  
    new Author { Name = "Chand Beniwal", Book = ".NET Best Practices", Price = 29.95 }  
};  
// Insert a collection of Author  
authors.InsertRange(2, newAuthors);

Here is the complete code example. 

using System;  
using System.Collections.Generic;  
  
public class Author  
{  
    // Auto-Initialized properties  
    public string Name { get; set; }  
    public string Book { get; set; }  
    public double Price { get; set; }  
    public string AuthorDetails()  
    {  
        return string.Format("{0} is an author of {1}. Price: ${2}",  
            Name, Book, Price);  
    }  
}  
  
  
class Program  
{  
    static void Main(string[] args)  
    {  
        Console.WriteLine("Hello List!");  
        ListIntro();  
    }  
  
    public static void ListIntro()  
    {  
        // Create a List of int  
        List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};  
        // Add more items to List  
        numbers.Add(30);  
        numbers.Add(35);  
        numbers.Add(40);  
        // Add a Range  
        int[] prime = new int[] { 1, 3, 5, 7, 11, 13};  
        numbers.AddRange(prime);  
  
        // Insert an item at index = 3  
        numbers.Insert(2, 22);  
        numbers.Insert(3, 33);  
  
        // Insert a collection     
        numbers.InsertRange(5, new int[] { 11, 22, 33, 44, 55});  
  
        // Read List items  
        foreach (int num in numbers)  
        {  
            Console.Write($"{num}, ");       
        }  
  
        Console.WriteLine();  
  
        // Create a List of strings   
        List<string> names = new List<string>()  
                            {"Mahesh Chand", "Neel Beniwal", "Chris Love" };  
        // Add more items to the list  
        names.Add("Author One");  
        names.Add("Author Two");  
        names.Add("Author Three");  
  
        // Add more items  
        names.AddRange(new string[] {"Raj Beniwal", "Allen O'neill" });  
  
        // Insert a string   
        names.Insert(3, "Mahesh Chand at 4th");  
  
        // Insert a collection of strings  
        names.InsertRange(2, new string[] {"Mahesh Beniwal", "Chand Beniwal", "Mahesh Chand Beniwal" });  
  
        // Read List items  
        foreach (string name in names)  
        {  
            Console.Write($"{name}, ");  
        }  
  
        Console.WriteLine();  
  
        // Create a List of objects  
        List<Author> authors = new List<Author>  
        {  
            new Author { Name = "Mahesh Chand", Book = "ADO.NET Programming", Price = 49.95 },  
            new Author { Name = "Neel Beniwal", Book = "Jump Ball", Price = 19.95 },  
            new Author { Name = "Chris Love", Book = "Practical PWA", Price = 29.95 }  
        };  
        // Add more items to the list  
        authors.Add(new Author { Name = "Mahesh Chand", Book = "Graphics with GDI+", Price = 49.95 });  
        authors.Add(new Author { Name = "Mahesh Chand", Book = "Mastering C#", Price = 54.95 });  
        authors.Add(new Author { Name = "Mahesh Chand", Book = "Jumpstart Blockchain", Price = 44.95 });  
  
        // Create a new List of Author  
        List<Author> duplicateAuthors = new List<Author>  
        {  
            new Author { Name = "Mahesh Chand", Book = "ADO.NET Programming", Price = 49.95 },  
            new Author { Name = "Neel Beniwal", Book = "Jump Ball", Price = 19.95 },  
            new Author { Name = "Chris Love", Book = "Practical PWA", Price = 29.95 }  
        };  
        // Add List using AddRange  
        authors.AddRange(duplicateAuthors);  
  
  
        // Insert an object  
        authors.Insert(3, new Author { Name = "Mahesh Beniwal", Book = "New Graphics with GDI+", Price = 49.95 } );  
  
        // Collection of Author  
        List<Author> newAuthors = new List<Author>  
        {  
            new Author { Name = "Mahesh Chand", Book = "Database Programming", Price = 49.95 },  
            new Author { Name = "Mahesh Chand Beniwal", Book = "C# Development", Price = 19.95 },  
            new Author { Name = "Chand Beniwal", Book = ".NET Best Practices", Price = 29.95 }  
        };  
        // Insert a collection of Author  
        authors.InsertRange(2, newAuthors);  
  
  
        // Read List objects  
        foreach (Author author in authors)  
        {  
            Console.WriteLine($"Author: {author.Name}:{author.Book}:{author.Price}");  
        }  
  
        Console.ReadKey();  
    }  
}

Further readings

Read this complete C# List Tutorial



Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.