gray laptop computer showing html codes in shallow focus photography

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

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...