Check if Two Dictionary Objects are Equal in C# Last Updated : 01 May, 2025 Comments Improve Suggest changes Like Article Like Report In C#, a Dictionary<TKey, TValue> is a collection of key-value pairs. When we work with dictionaries, we may need to check if two dictionaries are equal. This means they contain the exact same keys and values.In this article, we are going to learn how to check dictionary equality both by reference and by value in C#.Syntax:public virtual bool Equals (object obj);Parameter: obj: The object to compare with the current dictionary instance.Return Type: bool: It returns true, if the current dictionary and the specified object refer to the same instance, otherwise, it returns false.C# Program to Check Dictionary Equality by ReferenceThis method checks whether two dictionary variables point to the same object in memory.Example 1: Checking Reference Equality C# // C# program to check reference equality // of two dictionaries using System; using System.Collections.Generic; class Geeks { public static void Main() { // Creating a Dictionary Dictionary<string, string> d1 = new Dictionary<string, string>(); d1.Add("Sweta", "Noida"); d1.Add("Amiya", "Bengaluru"); // d1 refers to the same instance as d2 Dictionary<string, string> d2 = d1; // Checking reference equality Console.WriteLine(d1.Equals(d2)); } } OutputTrue Example 2: Checking Reference Inequality C# // C# program to show Equals returns false // for different instances with same content using System; using System.Collections.Generic; class Geeks { public static void Main() { // Creating first dictionary Dictionary<string, string> d1 = new Dictionary<string, string>(); d1.Add("Sweta", "Noida"); // Creating second dictionary with same content Dictionary<string, string> d2 = new Dictionary<string, string>(); d2.Add("Sweta", "Noida"); // Content is same, references are different Console.WriteLine(d1.Equals(d2)); } } OutputFalse C# Program to Check Dictionary Equality by ValueTo compare if two dictionaries have the same key-value pairs, here, we will use LINQ methods like Except().Example: Value-Based Dictionary Comparison C# // C# program to compare dictionary values using LINQ using System; using System.Collections.Generic; using System.Linq; class Geeks { public static void Main() { // Creating first dictionary Dictionary<string, string> d1 = new Dictionary<string, string>() { {"Sweta", "Noida"}, {"Amiya", "Bengaluru"} }; // Creating second dictionary with same // key-value pairs in different order Dictionary<string, string> d2 = new Dictionary<string, string>() { {"Amiya", "Bengaluru"}, {"Sweta", "Noida"} }; // Comparing dictionaries by content bool areEqual = d1.Count == d2.Count && !d1.Except(d2).Any(); Console.WriteLine("Are dictionaries equal by value? " + areEqual); } } OutputAre dictionaries equal by value? True Important Points:Use Equals() to check if two dictionary references point to the same object.Use value-based comparison logic like Except() and Count() when comparing actual content.We need to compare both keys and values to determine logical equality. Comment More infoAdvertise with us Next Article Check if Two Dictionary Objects are Equal in C# K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Generic-Namespace CSharp Dictionary Class Similar Reads C# | Check if two List objects are equal Equals(Object) Method which is inherited from the Object class is used to check if a specified List<T> object is equal to another List<T> object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return Value: 2 min read C# | Check if two HybridDictionary objects are equal Equals(Object) Method which is inherited from the Object class is used to check if a specified HybridDictionary object is equal to another HybridDictionary object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return 2 min read C# | Check if two Tuple Objects are equal A tuple is a data structure which gives you the easiest way to represent a data set. You can also check if the given tuple object is equal to the specified object or not using the Equals Method. This method will return true if the given tuple object is equal to the specified object, otherwise, retur 2 min read C# | Check if two ListDictionary objects are equal Equals(Object) Method which is inherited from the Object class is used to check if a specified ListDictionary object is equal to another ListDictionary object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return Valu 2 min read Check if two enums are equal or not in C# Enum.Equals(Object) Method is used to check whether the current instance is equal to a specified object or not. This method overrides ValueType.Equals(Object) to define how enumeration members are evaluated for equality. Syntax: public override bool Equals (object obj); Here, obj is an object to com 1 min read C# | Check if two BitArray objects are equal Equals(Object) Method which is inherited from the Object class is used to check if a specified BitArray object is equal to another BitArray object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return Value: This meth 2 min read C# | Check if two ArrayList objects are equal Equals(Object) Method which is inherited from the Object class is used to check whether the specified ArrayList object is equal to another ArrayList object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return Value: 2 min read C# | Check if two SortedList objects are equal Equals(Object) Method which is inherited from the Object class is used to check whether the specified SortedList object is equal to another SortedList object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return Value 2 min read C# | Check if two LinkedList<T> objects are equal Equals(Object) Method which is inherited from the Object class is used to check if a specified LinkedList<T> object is equal to another LinkedList<T> object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. R 2 min read Check if two SortedDictionary objects are equal in C# Equals(Object) Method which is inherited from the Object class is used to check if a specified SortedDictionary object is equal to another SortedDictionary object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return 2 min read Like