Home > Backend Development > C#.Net Tutorial > C# program showing usage of LINQ Aggregate() method

C# program showing usage of LINQ Aggregate() method

WBOY
Release: 2023-09-15 23:25:06
forward
845 people have browsed it

显示 LINQ Aggregate() 方法用法的 C# 程序

The Aggregate() method is a powerful LINQ method that allows you to perform reduction operations on a sequence of elements. This method can be used to perform calculations on a set of data, such as finding the sum, product, or maximum of a set of numbers. In this article, we will explore how to use the Aggregate() method in a C# program.

What is the Aggregate() method?

The Aggregate() method is a LINQ extension method that takes two parameters: a seed value and a function that performs a reduction operation on the sequence of elements. The seed value is the initial value for the operation, and the function specifies how to combine each element in the sequence with the previous result.

Syntax of Aggregate() method

public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func)
Copy after login

Example: Use the Aggregate() method to find the sum of a sequence of numbers

Let's look at an example of using the Aggregate() method to find the sum of a series of numbers.

using System.IO;
using System;
using System.Linq;

class Program {
   static void Main(string[] args) {
      int[] numbers = { 1, 2, 3, 4, 5 };
   
      int sum = numbers.Aggregate((x, y) => x + y);
   
      Console.WriteLine("The sum of the sequence is: {0}", sum);
   }
}
Copy after login

In this code, we have an array of integers called numbers. We use the Aggregate() method to calculate the sum of a sequence by passing a lambda expression to add two elements.

Output

The sum of the sequence is: 15
Copy after login

Example: Use the Aggregate() method to find the product of a sequence of numbers

Now, let us look at an example of how to find the product of a sequence of numbers using the Aggregate() method.

using System;
using System.Linq;

class Program {
   static void Main() {
      int[] numbers = { 1, 2, 3, 4, 5 };
      int product = numbers.Aggregate(1, (x, y) => x * y);
      Console.WriteLine("The product of the sequence is: {0}", product);
   }
}
Copy after login

In this code, we have an array of integers called numbers. We use the Aggregate() method to calculate the product of a sequence by passing an initial value of 1 and a lambda expression to multiply the two elements.

Output

The product of the sequence is: 120
Copy after login

in conclusion

The Aggregate() method is a powerful LINQ method that can be used to perform reduction operations on a sequence of elements. In this article, we explored how to use the Aggregate() method in a C# program to find the sum and product of a series of numbers.

The above is the detailed content of C# program showing usage of LINQ Aggregate() method. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template