Advertisement
ivandrofly

Aggregation Demo C#/CSharp

Dec 30th, 2015
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.64 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         int[] array = { 1, 2, 3, 4, 5 };
  9.         int result = array.Aggregate(AddNums);
  10.         // 1 + 2 = 3
  11.         // 3 + 3 = 6
  12.         // 6 + 4 = 10
  13.         // 10 + 5 = 15
  14.         Console.WriteLine(result);
  15.  
  16.         result = array.Aggregate((a, b) => b * a);
  17.         // 1 * 2 = 2
  18.         // 2 * 3 = 6
  19.         // 6 * 4 = 24
  20.         // 24 * 5 = 120
  21.         Console.WriteLine(result);
  22.     }
  23.  
  24.     static int AddNums(int a, int b)
  25.     {
  26.         var result = a + b;
  27.         Console.WriteLine($"a:{a} + b:{b} = {result}");
  28.         return result;
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement