ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

EntryEquals

Extension method for comparing dictionaries by elements (key pair values)

Source

public static bool EntryEquals<TKey,TValue>(this IDictionary<TKey, TValue> dict, IDictionary<TKey,TValue> other) where TValue : class
{
	foreach(KeyValuePair<TKey,TValue> kvp in dict)
	{
		if (other.ContainsKey (kvp.Key))
		{
			TValue otherValue = other[kvp.Key];
			if (!kvp.Value.Equals(otherValue)) return false;
		} else
		{
			return false;
		}
	}
	return true;
}

Example

Dictionary<int, string> d = new Dictionary<int, string> {{1,"one"}, {2,"two"}, {3,"three"}};
Dictionary<int, string> d2 = new Dictionary<int, string> { { 3, "three" }, { 1, "one" }, { 2, "two" } };

Debug.Assert(d.EntryEquals (d2));

Author: Witold Szymaniak

Submitted on: 4 jun. 2009

Language: C#

Type: System.Collections.Generic.IDictionary

Views: 4255