C# program to concat two or more Lists

CsharpProgrammingServer Side Programming

Set three lists −

// three lists
var list1 = new List<int>{3, 4};
var list2 = new List<int>{1, 2, 3};
var list3 = new List<int>{2, 5, 6};

Now, use the Concat mthod to concat the above lists −

var res1 = list1.Concat(list2);
var res2 = res1.Concat(list3);

Here is the complete code −

Example

 Live Demo

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

public class Demo {
   public static void Main() {

      // three lists
      var list1 = new List<int>{3, 4};
      var list2 = new List<int>{1, 2, 3};
      var list3 = new List<int>{2, 5, 6};

      // concat
      var res1 = list1.Concat(list2);
      var res2 = res1.Concat(list3);

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

Output

3
4
1
2
3
2
5
6
raja
Published on 31-Aug-2018 16:29:20
Advertisements