How do I merge two C# Lists?

How do I join two C# lists is one of the most frequently asked questions. You use the AddRange method to merge two C# lists. The AddRange method of C# List takes a collection of similar objects. For example, the following code snippet creates two lists of int types. The AddRange method adds all items of evenList to the end of the oddList.


public class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine("Add two C# Lists");

        // Create a list of 5 odd numbers
        List<int> oddList = new() { 1, 3, 5, 7, 9 };

        // Create a list of 5 event numbers
        List<int> evenList = new() { 2, 4, 6, 8, 10 };

        // Merge evenList to oddList
        oddList.AddRange(evenList);

        // Display all 10 numbers
        foreach (int num in oddList)
        {
            Console.WriteLine(num);
        }

        Console.ReadKey();
    }
}

Note: You cannot merge two C# lists of different types. For example, the following code snippet tried to merge a list of strings with a list of numbers.

// Create a list of strings
List<string> authors = new() { "Cow", "Camel", "Elephant" };
oddList.AddRange(authors);

The C# compiler throws an error - Cannot convert a string to an int type.

Want to learn more about C# List?

Here is a detailed tutorial > The Complete C# List Tutorial 

Next Recommended Reading Merge Sort Algorithm in C#

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.