This is not as clear as other methods, but it should work fine:
dictionary.Keys.ToList().ForEach(i => dictionary[i] = 0);
My other alternative would be to make the ForEach extension method look like this:
public static class MyExtensions
{
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (var item in items)
{
action(item);
}
}
}
Then use it as follows:
dictionary.ForEach(kvp => kvp.Value = 0);
This will not work in this case, since a value cannot be assigned.
source
share