Q:

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

belongs to collection: C# LINQ Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# LINQ Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to calculate the sum of array elements ... >>
<< C# program to reverse the list of cities using Lin...