C # example of using accumulators for Lambda expressions,
Using System; using System. collections. generic; using System. linq; using System. text; namespace LambdaSample {class Program {static void Main (string [] args) {int [] myIntArray = {1, 2, 3, 4, 5 }; // The accumulators calculate the total array int result = myIntArray. aggregate (paramA, paramB) => paramA + paramB); int result1 = myIntArray. aggregate <int> (paramA, paramB) => paramA + paramB); int result2 = myIntArray. aggregate <int, int> (0, (paramA, paramB) => paramA + paramB); // recursively calculates the total array int result3 = count (5); Console. writeLine ("The Result is:" + result); Console. writeLine ("The Result1 is:" + result1); Console. writeLine ("The Result2 is:" + result2); Console. writeLine ("The Result3 is:" + result3); string [] myStrArray = {"Admin", "Change", "ByPage "}; // concatenate A string Array and string strResult = myStrArray. aggregate (paramA, paramB) => paramA + "" + paramB); // The total length of the string array is calculated as int result4 = myStrArray. aggregate <string, int> (0, (a, B) => a + B. length); // string Array splicing 2, adding the string "Some curries:" string strResult1 = myStrArray. aggregate <string, string> ("Some curries:", (a, B) => a + "" + B ); // The total length of the string array is calculated as 2 int result5 = myStrArray. aggregate <string, string, int> ("Some curries:", (a, B) => a + "" + B, a =>. length); Console. writeLine ("The StrResult is:" + strResult); Console. writeLine ("The Result4 is:" + result4); Console. writeLine (strResult1); Console. writeLine ("The Result5 is:" + result5); Console. readKey ();} static int count (int n) {if (n = 1) {return 1;} return n + count (n-1 );}}}
Source code of this example is taken from Chapter 5th of C # Getting Started classic 14th (page 1)