Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to compare two dictionaries in C#?
Comparing two dictionaries in C# involves checking whether they contain the same key-value pairs. This can be done through several approaches, from manual iteration to using LINQ methods for more concise solutions.
Using Manual Iteration
The most straightforward approach is to manually iterate through one dictionary and check if the other contains matching key-value pairs −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
// Dictionary One
IDictionary<int, int> d1 = new Dictionary<int, int>();
d1.Add(1, 97);
d1.Add(2, 89);
d1.Add(3, 77);
d1.Add(4, 88);
// Dictionary Two
IDictionary<int, int> d2 = new Dictionary<int, int>();
d2.Add(1, 97);
d2.Add(2, 89);
d2.Add(3, 77);
d2.Add(4, 88);
bool equal = false;
if (d1.Count == d2.Count) {
equal = true;
foreach (var pair in d1) {
int value;
if (d2.TryGetValue(pair.Key, out value)) {
if (value != pair.Value) {
equal = false;
break;
}
} else {
equal = false;
break;
}
}
}
Console.WriteLine("Dictionary One elements: " + d1.Count);
Console.WriteLine("Dictionary Two elements: " + d2.Count);
Console.WriteLine("Are dictionaries equal? " + equal);
}
}
The output of the above code is −
Dictionary One elements: 4 Dictionary Two elements: 4 Are dictionaries equal? True
Using LINQ SequenceEqual
A more concise approach uses LINQ's SequenceEqual method to compare dictionary contents −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
var dict1 = new Dictionary<string, int> {
{"apple", 5},
{"banana", 3},
{"orange", 8}
};
var dict2 = new Dictionary<string, int> {
{"apple", 5},
{"banana", 3},
{"orange", 7}
};
var dict3 = new Dictionary<string, int> {
{"apple", 5},
{"banana", 3},
{"orange", 8}
};
bool equal1 = dict1.OrderBy(x => x.Key).SequenceEqual(dict2.OrderBy(x => x.Key));
bool equal2 = dict1.OrderBy(x => x.Key).SequenceEqual(dict3.OrderBy(x => x.Key));
Console.WriteLine("dict1 equals dict2: " + equal1);
Console.WriteLine("dict1 equals dict3: " + equal2);
}
}
The output of the above code is −
dict1 equals dict2: False dict1 equals dict3: True
Using LINQ All Method
Another efficient approach uses the All method to verify that every key-value pair in one dictionary exists in the other −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
var students1 = new Dictionary<int, string> {
{101, "John"},
{102, "Alice"},
{103, "Bob"}
};
var students2 = new Dictionary<int, string> {
{101, "John"},
{102, "Alice"},
{103, "Bob"}
};
var students3 = new Dictionary<int, string> {
{101, "John"},
{102, "Alice"},
{104, "Charlie"}
};
bool areEqual1 = students1.Count == students2.Count &&
students1.All(pair => students2.ContainsKey(pair.Key) &&
students2[pair.Key].Equals(pair.Value));
bool areEqual2 = students1.Count == students3.Count &&
students1.All(pair => students3.ContainsKey(pair.Key) &&
students3[pair.Key].Equals(pair.Value));
Console.WriteLine("students1 equals students2: " + areEqual1);
Console.WriteLine("students1 equals students3: " + areEqual2);
}
}
The output of the above code is −
students1 equals students2: True students1 equals students3: False
Comparison of Approaches
| Approach | Performance | Readability | Best For |
|---|---|---|---|
| Manual Iteration | Good | Verbose | Simple scenarios, custom logic |
| LINQ SequenceEqual | Moderate | Concise | When order matters after sorting |
| LINQ All Method | Good | Clean | Most scenarios, functional style |
Conclusion
Comparing dictionaries in C# can be accomplished through manual iteration for fine control, or LINQ methods like SequenceEqual and All for more concise code. The LINQ All method provides the best balance of readability and performance for most dictionary comparison scenarios.
