C # example of using accumulators for Lambda expressions,

Source: Internet
Author: User

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)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.