TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Aggregate: Use Lambda to Accumulate Value

Use the Aggregate extension method from System.Linq to accumulate a result.
Aggregate applies a method to each element. It applies a function to each successive element. With this extension method, we act upon the aggregate of all previous elements. This makes certain methods, such as sum, possible.LINQ
Example. There are 2 example calls to the Aggregate method here. The first call uses a Func that specifies that the accumulation should be added to the next element. This simply results in the sum of all the elements in the array.Sum

However: The second call is more interesting. It multiplies the accumulation with the next element.

Accumulate: With Aggregate we "accumulate" values, meaning we build up values from each previous function call.

C# program that calls Aggregate method using System; using System.Linq; class Program { static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int result = array.Aggregate((a, b) => b + a); // 1 + 2 = 3 // 3 + 3 = 6 // 6 + 4 = 10 // 10 + 5 = 15 Console.WriteLine(result); result = array.Aggregate((a, b) => b * a); // 1 * 2 = 2 // 2 * 3 = 6 // 6 * 4 = 24 // 24 * 5 = 120 Console.WriteLine(result); } } Output 15 120
Func. The Aggregate method receives a higher-order procedure of type Func. The Func receives 2 arguments and returns a third. This is the accumulator function. Funcs can be used in many programs.Func
Extensions. Aggregate is the first method listed in Visual Studio on many collections (like arrays). This is confusing for those new to the C# language. Visual Studio is presenting extension methods.

So: The list of methods, starting with Aggregate, are methods from a namespace called System.Linq.

Extension
They are not specific to arrays or Lists. Instead, these extension methods act upon any collection that implements the IEnumerable interface. We can use these methods to make certain tasks simpler.IEnumerable

However: In my experience, Aggregate is one of the least useful extensions. Try using ToArray or ToList to gain familiarity.

ToArrayToList
Summary. Aggregate is a declarative way of applying an accumulator function to a collection of elements. This means you can multiply or add all elements together. More complex accumulator functions can be used.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf