読者です 読者をやめる 読者になる 読者になる

Extension Method - IDictionary

C#
using System;
using System.Collections.Generic;

namespace Shand.Extension
{
    public static partial class Extensions
    {
        public static void ForEach<Key, Value>(this IDictionary<Key, Value> source, Action<Key, Value> action)
        {
            foreach (var item in source)
            {
                action(item.Key, item.Value);
            }
        }
    }
}


Ruby っぽい ForEach

var dict = new Dictionary<string, int> {
                {"Akira",  23},
                {"Johnny", 38},
                {"Millia", 16},
            };

dict.ForEach((key, value) =>
{
    Console.WriteLine("{0},{1}", key, value);
});