ConcurrentDictionarydict = new ConcurrentDictionary (); dict.TryAdd("a", 1); dict.TryAdd("b", 2); dict.TryAdd("c", 3); int sum = dict.Aggregate(0, (acc, pair) => acc + pair.Value); Console.WriteLine(sum); // outputs 6
using System.Linq; ConcurrentDictionaryIn this example, we create a ConcurrentDictionary with two key/value pairs. We use the Aggregate method to concatenate the keys with a "-" separator and store the result in the variable `result`. The ConcurrentDictionary Aggregate method is part of the .NET Framework Class Library. No additional package library is needed.dict = new ConcurrentDictionary (); dict.TryAdd(1, "hello"); dict.TryAdd(2, "world"); string result = dict.Aggregate("", (acc, pair) => acc + pair.Key.ToString() + "-"); result = result.TrimEnd('-'); Console.WriteLine(result); // outputs "1-2"