Let’s assume that I have a collection of strings, like so:
IList<string> myList = new List<string>(){"one", "two", "three"};
Using myList.Aggregate I would like to end up with “‘one’, ‘two’, ‘three'” (including single quotes)
Does someone have a sleak way of doing this, only using the Aggregate function? I was thinking something in the lines of
myList.Aggregate((increment, seed) => string.Concat(increment, ", ", seed));
but that’s only half the solution.
Any reason to use
Aggregaterather than the simplerstring.Join?(Add a
ToArraycall if you’re using .NET 3.5.)You can implement
string.JoinusingAggregate(ideally using aStringBuilder) but it’s not terribly pleasant. Assuming a non-empty collection, I think this should do it: