I wanna declare new extension method which similar to List.ForEach Method.
What I wanna archive:
var dict = new Dictionary<string, string>()
{
   { "K1", "V1" },
   { "K2", "V2" },
   { "K3", "V3" },
};
dict.ForEach((x, y) => 
{
   Console.WriteLine($"(Key: {x}, value: {y})");
});
How can I do that?
You can write an extension method easily:
public static class LinqExtensions
{
    public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invoke)
    {
        foreach(var kvp in dictionary)
            invoke(kvp.Key, kvp.Value);
    }
}
Using like this:
dict.ForEach((x, y) => 
{
   Console.WriteLine($"(Key: {x}, value: {y})");
});
Produces
Key: K1, value: V1
Key: K2, value: V2
Key: K3, value: V3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With