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

Here, we are going to learn about the Linq Aggregate() method and its C# implementation.
Submitted by Nidhi, on August 31, 2020

Here we will create an array of strings for courses, and concatenate comma ", " between all courses and then combined all courses using Linq Aggregate() method.

Program:

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

//C# Program to demonstrate Linq Aggregate() method.

using System;
using System.Linq;

class LinqDemo
{
    static void Main(string[] args)
    {
        string[] Courses = { "BCA", "MCA", "MBA", "MA", "CA", "BBA" };

        string rstString = "";
        
        rstString=Courses.Aggregate((s1, s2) => s1 + ", " + s2);

        Console.WriteLine(rstString);
    }
}

Output:

BCA, MCA, MBA, MA, CA, BBA
Press any key to continue . . .

Explanation:

In the above program, we created an array of strings that contains courses. And then use the Aggregate() method to combine all courses and separate all courses using comma (,) operator in the final string. Here Aggregate() method will return a combined single string.

To use the Aggregate() method, it is mandatory to import "System.Linq" namespace.

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.