HomeCSharpUsing Aggregate Function on Arrays in C#

Using Aggregate Function on Arrays in C#

If you have got an array of integers and you want to multiple the values inside the array and display its value without using for loop  , here’s one of the ways to achieve it using the LINQ and Lambda expression .

Using Aggregate Function on Arrays in C#

Assuming the array of integer has the following values

int[] marks = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

You can multiply each of the values inside the integer array without using any for loop or foreach with the below code.

var aggregate = marks.Aggregate((a, b) => a * b);

Make sure you include the namespace to get it working 🙂

using System.Linq;
int[] marks = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var aggregate = marks.Aggregate((a, b) => a * b);

MessageBox.Show(aggregate.ToString());

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...