日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Aggregate累加器

發布時間:2025/5/22 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Aggregate累加器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天看東西的時候看見這么個擴展方法Aggregate(累加器)很是陌生,于是乎查了查,隨手記錄一下。
直接看一個最簡答的版本,其他版本基本沒什么區別,需要的時候可看一下

public static TSource Aggregate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func
)

這個方法的功能其實是對,可枚舉的IEnumerable<TSource>某種數據,從前兩個遍歷對象開始逐個,作為輸入進行自定
義的操作,這個方法還是蠻有用的,看幾個例子。

private void button7_Click(object sender, EventArgs e){string sentence = "the quick brown fox jumps over the lazy dog";string[] words = sentence.Split(' ');Func<string, string, string> temp = test;string reversed = words.Aggregate("",temp);//string reversed=words.Aggregate((workingSentence, next) =>next + " " + workingSentence);MessageBox.Show(reversed);}public string test(string para1, string para2){return para2 + " " + para1;}

  這里我沒用msdn直接提供的lambda方式,目的就是為了方便調試查看下。先給出結果吧dog lazy the over jumps fox brown quick the

其執行過程是這樣滴,第一次 para1 為the ,para2為 quick,返回了 quick the, 并且作為下次的 para1,para2 為brown ,如此依次的遍歷執行下去,直至結束。

  再給一個例子可以看出許多應用。計算數據中的整數的個數

private void button8_Click(object sender, EventArgs e){int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };int numEven = ints.Aggregate(0, (total, next) =>next % 2 == 0 ? total + 1 : total);MessageBox.Show("The number of even integers is: " + numEven);}

  恩恩,這都是msdn直接給出的例子,那么很多類似的統計或者計算累的需求是不是就可以考慮下Aggregate了。

轉載于:https://www.cnblogs.com/superCow/p/3804134.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Aggregate累加器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。