How to compare two Dictionaries in C
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Comparing two dictionaries in C# is less about syntax and more about deciding what equality should mean. Do you only care that the same keys exist, or do you need both keys and values to match exactly? Once that definition is clear, the implementation is straightforward and usually does not require anything more complicated than a count check and key-by-key comparison.
What Dictionary Equality Usually Means
For most applications, two dictionaries are equal if all of these are true:
- both contain the same number of entries
- every key in the first dictionary exists in the second
- the value for each matching key is equal
Dictionary order does not matter. A Dictionary<TKey, TValue> is not a sequence, so using sequence-based comparison carelessly can lead to wrong results if the enumeration order differs.
A Practical Equality Method
The safest general-purpose approach is to iterate through one dictionary and check the other with TryGetValue.
Example usage:
This prints True, even though the insertion order was different.
Comparing Only Keys or Only Values
Sometimes full equality is more than you need. If you only care about keys, compare the key sets.
That approach sorts keys first so order does not distort the result. For large dictionaries, a hash-set-based comparison may be cheaper than sorting.
If you only care about values, the meaning becomes trickier because duplicates matter. A dictionary is key-based by nature, so value-only comparison is usually a specialized requirement rather than the default.
Custom Value Comparisons
The generic method above uses EqualityComparer<TValue>.Default, which works well for primitive types, strings, records, and types that implement meaningful equality.
For complex reference types, you may need to compare selected properties instead. In that case, pass a custom comparer.
That makes the comparison logic reusable without rewriting the loop.
Why SequenceEqual Can Mislead
A common shortcut is converting dictionaries to sequences and calling SequenceEqual. That only works reliably if both sequences are ordered in the same way first.
This can be acceptable for small dictionaries, but it does extra sorting work and is less direct than TryGetValue.
The key point is that raw dictionary enumeration order is not a good basis for equality.
Common Pitfalls
The most common mistake is comparing dictionaries with SequenceEqual without ordering them first. Two equal dictionaries can enumerate differently.
Another issue is relying on reference equality for value objects that should be compared by content. If TValue is a custom class without a proper equality implementation, dictionary comparison can appear to fail for identical-looking data.
Developers also sometimes forget null handling. Comparison helpers should define what happens when either input is null.
Finally, choose the comparison goal carefully. Comparing only keys, only values, or the full dictionary are three different problems and should not share the same helper by accident.
Summary
- Full dictionary equality means same keys and equal values, regardless of order.
- '
TryGetValuewith a count check is the clearest comparison strategy.' - Use
EqualityComparer<TValue>.Defaultunless your value type needs custom logic. - Do not rely on raw enumeration order for equality checks.
- Decide first whether you need key equality, value equality, or complete equality.
Related reading
- How to compare two ListString to each other?
- How to compress pointer ? eg. arbitrary bit pointer
- How to compute intersection of N sorted sets?
- How to concatenate join items in a list to a single string
- How to completely uninstall Visual Studio 2010?
- How to concatenate two IEnumerableT into a new IEnumerableT?
- How to concatenate two dictionaries to create a new one?
- How to configure Kafka to behave like a FiFo queue?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.