두 사전을 비교하려면 먼저 두 사전을 설정하십시오 -
사전 원
IDictionary<int, int> d = new Dictionary<int, int>();
d.Add(1,97);
d.Add(2,89);
d.Add(3,77);
d.Add(4,88);
// Dictionary One elements
Console.WriteLine("Dictionary One elements: "+d.Count); 사전 원
IDictionary<int, int> d2 = new Dictionary<int, int>();
d2.Add(1,97);
d2.Add(2,89);
d2.Add(3,77);
d2.Add(4,88);
// Dictionary Two elements
Console.WriteLine("Dictionary Two elements: "+d2.Count); 이제 그것들을 비교해보자 -
bool equal = false;
if (d.Count == d2.Count) { // Require equal count.
equal = true;
foreach (var pair in d) {
int value;
if (d2.TryGetValue(pair.Key, out value)) {
if (value != pair.Value) {
equal = false;
break;
}
} else {
equal = false;
break;
}
}
} 위의 두 사전을 비교합니다. 이제 콘솔을 인쇄하면 결과가 True가 됩니다. 이는 두 사전이 동일한 값을 갖는다는 것을 의미합니다.