C# - Example of LINQ Concat() Method

Learn about the Linq Concat() method and its C# implementation with an example. By Nidhi Last updated : April 01, 2023

Here we will append one list to another list using Linq Concat() method and print the result using the "foreach" loop on the console screen.

C# program to demonstrate the example of LINQ Concat() method

The source code to demonstrate the Linq Concat() method, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# Program to demonstrate the Linq Concat() method.

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

class Demo
{
    static void Main(string[] args)
    {
        List<string> List1 = new List<string>() { "Abc", "Pqr", "Lmn", "Xyz" };
        List<string> List2 = new List<string>() { "Abc", "pqr" , "KLP" };

        var result = List1.Concat(List2);

        foreach (string value in result)
        {
            Console.WriteLine(value + " ");
        } 
    }
}

Output

Abc
Pqr
Lmn
Xyz
Abc
pqr
KLP
Press any key to continue . . .

Explanation

In the above program, we create two lists list1 and list2 of string values. Here we used Linq Concat() method to append the value of list2 with list1.

var result = List1.Concat(List2);

foreach (string value in result)
{
    Console.WriteLine(value + " ");
} 

In the above code, we appended the values of list2 to the list1 and assigned to the result then print them on the console screen.

C# LINQ Programs »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.