
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to compare two dictionaries in C#?
To compare two dictionaries, firstly set the two dictionaries −
Dictionary One
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);
Dictionary One
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);
Now let us compare them −
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; } } }
The above compares two dictionaries. Now print console and the result would be True. That would mean both the dictionaries have the same values.
- Related Articles
- How do we compare two dictionaries in Python?
- Python Program to compare elements in two dictionaries
- How to merge two Python dictionaries in a single expression?
- How to find difference in keys contained in two Python dictionaries?
- Python program to merge two Dictionaries
- C# program to merge two Dictionaries
- How can we merge two Python dictionaries?
- How to compare two dates in Java?
- How to compare two numbers in JavaScript?
- How to Compare two String in Java?
- How to compare two arrays in Java?
- How to compare two arrays in C#?
- How to compare two Dates in C#?
- How to compare two tuples in C#?
- How to compare two objects in JavaScript?

Advertisements