Using HashCode in .NETFramework
How to use HashCode type in full .NET, which doesn't support it
So, I just learned that not even in .net472/net48, you can leverage the cool HashCode type from .NETCore. It is mentioned in docs as available in ".NET Platform Extensions 3.1", which isn't quite clear what it means. After a quick search here and there, I found out the Microsoft.Bcl.HashCode package, which seems to be one such ".NET platform extension" (although the version # doesn't match either :/).
With that, I can now make a KeyValuePairComparer<TKey, TValue>to check for dictionaries equality like:
1
public class KeyValuePairComparer<TKey, TValue> : IEqualityComparer<KeyValuePair<TKey, TValue>>
2
{
3
public static KeyValuePairComparer<TKey, TValue> Default { get;} = new KeyValuePairComparer<TKey, TValue>();
4
5
public bool Equals(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y) => object.Equals(x.Key, y.Key) && object.Equals(x.Value, y.Value);
6
7
public int GetHashCode(KeyValuePair<TKey, TValue> obj) => HashCode.Combine(obj.Key, obj.Value);
8
}
Copied!
To be used as:
1
IReadOnlyDictionary<string, string> first = ...
2
IReadOnlyDictionary<string, string> second = ...
3
4
return first.SequenceEquals(second, KeyValuePairComparer<string, string>.Default);
Copied!
Last modified 1mo ago
Copy link