I want to compare in C# two dictionaries with as keys a string and as value a list of ints. I assume two dictionaries to be equal when they both ha
I know this question already has an accepted answer, but I'd like to offer an even simpler alternative:
using System.Linq;
using System.Collections.Generic;
namespace Foo
{
public static class DictionaryExtensionMethods
{
public static bool ContentEquals(this Dictionary dictionary, Dictionary otherDictionary)
{
return (otherDictionary ?? new Dictionary())
.OrderBy(kvp => kvp.Key)
.SequenceEqual((dictionary ?? new Dictionary())
.OrderBy(kvp => kvp.Key));
}
}
}