VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/check-if-two-dictionary-objects-are-equal-in-c-sharp/

⇱ Check if Two Dictionary Objects are Equal in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if Two Dictionary Objects are Equal in C#

Last Updated : 11 Jul, 2025

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 Reference

This method checks whether two dictionary variables point to the same object in memory.

Example 1: Checking Reference Equality


Output
True


Example 2: Checking Reference Inequality


Output
False


C# Program to Check Dictionary Equality by Value

To compare if two dictionaries have the same key-value pairs, here, we will use LINQ methods like Except().

Example: Value-Based Dictionary Comparison


Output
Are 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

Explore