How To Merge Two C# List?

In this article, I will explain, how we merge the two C# lists. We can use the AddRange() method to merge two C# lists.

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MergeList
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> List1 = new List<int>() { 1, 3, 5, 7, 9 };
            List<int> List2 = new List<int>() { 2, 4, 6, 8, 10 };

            List1.AddRange(List2);

            foreach (int i in List1)
            {
                Console.WriteLine(i);
            }

            Console.ReadKey();
        }
    }
}

In the above example, I have created two lists of type int. The AddRange() method will List2 to List1.

Note:

We cannot merge two different types of lists.

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories