.NET Framework Agregado (pliegue)


Ejemplo

Generando un nuevo objeto en cada paso:

var elements = new[] {1,2,3,4,5};

var commaSeparatedElements = elements.Aggregate(
    seed: "",
    func: (aggregate, element) => $"{aggregate}{element},");
    
Console.WriteLine(commaSeparatedElements);  //1,2,3,4,5,

Usando el mismo objeto en todos los pasos:

var commaSeparatedElements2 = elements.Aggregate(
    seed: new StringBuilder(),
    func: (seed, element) => seed.Append($"{element},"));
    
Console.WriteLine(commaSeparatedElements2.ToString());  //1,2,3,4,5,

Usando un selector de resultados:

var commaSeparatedElements3 = elements.Aggregate(
    seed: new StringBuilder(),
    func: (seed, element) => seed.Append($"{element},"),
    resultSelector: (seed) => seed.ToString());
Console.WriteLine(commaSeparatedElements3);  //1,2,3,4,5,

Si se omite una semilla, el primer elemento se convierte en la semilla:

var seedAndElements = elements.Select(n=>n.ToString());
var commaSeparatedElements4 = seedAndElements.Aggregate(
    func: (aggregate, element) => $"{aggregate}{element},");

Console.WriteLine(commaSeparatedElements4);  //12,3,4,5,