Languages
[Edit]
EN

C# / .NET - combine multiple lists

0 points
Created by:
aaliyah
471

In this article, we would like to show you how to combine multiple lists in C#.

Quick solution:

List<string> list1 = new List<string> { "A", "B" };
List<string> list2 = new List<string> { "C", "D" };
List<string> list3 = new List<string> { "E", "F" };

List<string> combinedList = list1.Concat(list2).Concat(list3).ToList();

 

Practical example

In this example, we use Concat() method from System.Linq to combine multiple lists into one.

using System;
using System.Linq;
using System.Collections.Generic;

public static class Program
{
    public static void Main(string[] args)
    {
        List<string> list1 = new List<string> { "A", "B" };
        List<string> list2 = new List<string> { "C", "D" };
        List<string> list3 = new List<string> { "E", "F" };

        List<string> combinedList = list1.Concat(list2).Concat(list3).ToList();

        foreach (string items in combinedList)
            Console.WriteLine(items);
    }
}

Output:

A
B
C
D
E
F

References

  1. Enumerable.Concat<TSource>() Method (System.Linq) | Microsoft Docs
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join