Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

IEnumerable.Aggregateメソッド

Enumerable.Aggregate(TSource) メソッド (IEnumerable(TSource), Func(TSource, TSource, TSource)) (System.Linq)
シーケンスにアキュムレータ関数を適用します。

ソースコード

using System;
using System.Linq;

namespace LinqSample {
    class Program {
        static void Main(string[] args) {
            int[] numbers = new int[] { 1, 2, 3, 4, 5 };
            // シーケンスをラムダ式に従って累計します。
            // (5 - (4 - (3 - (2 - 1)))) = 3
            int calculated = numbers.Aggregate((current, next) => next - current);
            Console.WriteLine(calculated);
            Console.ReadKey();
        }
    }
}

実行結果