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

Here, we are going to learn about the Linq Concat() method and its C# implementation with an example.
Submitted by Nidhi, on August 28, 2020

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.

Program:

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 »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.