C#关于IEnumerable <T> .Aggregate

ret*_*ide 21 c# linq aggregate

我做了一些测试IList<T>.Aggregate(),但答案对我来说没有意义.

List<int> Data1 = new List<int> { 1,0,0,0,0};

var result = Data1.Aggregate<int>((total, next) => total + total);
Run Code Online (Sandbox Code Playgroud)

结果是16.

我期待它32.

谁能解释一下?

SLa*_*aks 21

Aggregate不会为列表中的第一个元素运行其回调.而是,第一个元素用作accumulator(total)的初始值.
因此,您的回调仅运行四次,而不是五次,而2 4 = 16次.