A library like Python's collections.Counter library for C# -> Getting the difference of values between two dictionary objects in C#
Question: This is how I would create a Dictionary in C#.

    
    
       Dictionary<string, int> d = new Dictionary<string, int>()
        {
            {"cheese", 2},
            {"cakes", 1},
            {"milk", 0},
            {"humans", -1}  // This one's for laughs
        };
    

In Python if you have a dictionary like so:

    
    
    from collections import Counter
    
    my_first_dict = {
        "cheese": 1,
        "cakes": 2,
        "milk": 3,
    }
    
    my_second_dict = {
        "cheese": 0,
        "cakes": 1,
        "milk": 4,
    }
    
    print Counter(my_first_dict) - Counter(my_second_dict)
    
    >>> Counter({'cheese': 1, 'cakes': 1})
    

As you can see, `Counter` is very useful when comparing dictionary objects.

Is there a library in C#, that will allow me to do something similar to this,
or do I have to code it from scratch?


Answer: You can join the two dictionaries together and then create a new one based on
the given operation with only a few lines of code:

    
    
    Dictionary<string, int> d1 = new Dictionary<string, int>();
    Dictionary<string, int> d2 = new Dictionary<string, int>();
    
    var difference = d1.Join(d2, pair => pair.Key, pair => pair.Key, (a, b) => new
    {
        Key = a.Key,
        Value = a.Value - b.Value,
    })
    .Where(pair => pair.Value > 0)
    .ToDictionary(pair => pair.Key, pair => pair.Value);
    

There is no system class that you've shown that wraps a dictionary an provides
a `-` operator for them, but you can make your own if you want easily enough:

    
    
    public class Counter<T> : IEnumerable<KeyValuePair<T, int>>
    {
        private IEnumerable<KeyValuePair<T, int>> sequence;
        public Counter(IEnumerable<KeyValuePair<T, int>> sequence)
        {
            this.sequence = sequence;
        }
    
        public static Counter<T> operator -(Counter<T> first, Counter<T> second)
        {
            return new Counter<T>(first.Join(second
                , pair => pair.Key, pair => pair.Key, (a, b) =>
                    new KeyValuePair<T, int>(a.Key, a.Value - b.Value))
                .Where(pair => pair.Value > 0));
        }
    
        public IEnumerator<KeyValuePair<T, int>> GetEnumerator()
        {
            return sequence.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

