Friday, January 20, 2012

using .Aggregate in C#

foreach (KeyValuePair<string, string> keyValuePair in dictionary) concatenatedValues += keyValuePair.Value;

 
 

I was able to refactor the line above into the line below by just following ReSharper's suggestion.

concatenatedValues = dictionary.Aggregate("", (current, keyValuePair) => current + keyValuePair.Value);

 
 

I'm still inclined to think in terms of the old foreach thing and then see what I may make of it that is more modern. .Aggregate is a first for me.

No comments:

Post a Comment