Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:22:22+00:00 2026-06-14T15:22:22+00:00

I have two dictionaries containing a string key and then an object. The object

  • 0

I have two dictionaries containing a string key and then an object. The object contains five fields. Is there an elegant way to ensure both dictionaries first contain the same keys and then if this is correct, contain the same five fields per object?

Would the two dictionaries have the same in-built hashcode or something?

EDIT, doesn’t appear to be working for the following code:

Dictionary<string, MyClass> test1 = new Dictionary<string, MyClass>();
Dictionary<string, MyClass> test2 = new Dictionary<string, MyClass>();

MyClass i = new MyClass("", "", 1, 1, 1, 1);
MyClass j = new MyClass("", "", 1, 1, 1, 1);

test1.Add("1", i);
test2.Add("1", j);

bool equal = test1.OrderBy(r => r.Key).SequenceEqual(test2.OrderBy(r => r.Key));

class MyClass
{
    private string a;
    private string b;
    private long? c;
    private decimal d;
    private decimal e;
    private decimal f;

    public MyClass(string aa, string bb, long? cc, decimal dd, decimal ee, decimal ff)
    {
        a= aa;
        b= bb;
        c= cc;
        d= dd;
        e= ee;
        f= ff;
    }

this returns false?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:22:23+00:00Added an answer on June 14, 2026 at 3:22 pm

    First you have to override Equals and GetHashCode method in your class, otherwise comparison will be performed on references instead of actual values. (The code to override Equals and GetHashCode is provided at the end), after that you can use:

    var result = (dic1 == dic2) || //Reference comparison (if both points to same object)
                 (dic1.Count == dic2.Count && !dic1.Except(dic2).Any());
    

    Since the order in which items in Dictionary are returned is undefined, you can not rely on Dictionary.SequenceEqual (without OrderBy).

    You can try:

    Dictionary<string, object> dic1 = new Dictionary<string, object>();
    Dictionary<string, object> dic2 = new Dictionary<string, object>();
    dic1.Add("Key1", new { Name = "abc", Number = "123", Address = "def", Loc = "xyz" });
    dic1.Add("Key2", new { Name = "DEF", Number = "123", Address = "def", Loc = "xyz" });
    dic1.Add("Key3", new { Name = "GHI", Number = "123", Address = "def", Loc = "xyz" });
    dic1.Add("Key4", new { Name = "JKL", Number = "123", Address = "def", Loc = "xyz" });
    
    dic2.Add("Key1",new { Name = "abc",Number=  "123", Address= "def", Loc="xyz"});
    dic2.Add("Key2", new { Name = "DEF", Number = "123", Address = "def", Loc = "xyz" });
    dic2.Add("Key3", new { Name = "GHI", Number = "123", Address = "def", Loc = "xyz" });
    dic2.Add("Key4", new { Name = "JKL", Number = "123", Address = "def", Loc = "xyz" });
    
    
    bool result = dic1.SequenceEqual(dic2); //Do not use that
    

    Most of the time the above will return true, but one can’t really rely on that due to unordered nature of Dictionary.

    Since SequenceEqual will compare the order as well, therefore relying on only SequenceEqual could be wrong. You have to use OrderBy to order both dictionaries and then use SequenceEqual like:

    bool result2 = dic1.OrderBy(r=>r.Key).SequenceEqual(dic2.OrderBy(r=>r.Key));
    

    But that will involve multiple iterations, once for ordering and the other for comparing each element using SequenceEqual.

    Code for overriding Equals and GetHashCode

    private class MyClass
    {
        private string a;
        private string b;
        private long? c;
        private decimal d;
        private decimal e;
        private decimal f;
    
        public MyClass(string aa, string bb, long? cc, decimal dd, decimal ee, decimal ff)
        {
            a = aa;
            b = bb;
            c = cc;
            d = dd;
            e = ee;
            f = ff;
        }
    
        protected bool Equals(MyClass other)
        {
            return string.Equals(a, other.a) && string.Equals(b, other.b) && c == other.c && e == other.e && d == other.d && f == other.f;
        }
    
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((MyClass)obj);
        }
    
        public override int GetHashCode()
        {
            unchecked
            {
                var hashCode = (a != null ? a.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ (b != null ? b.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ c.GetHashCode();
                hashCode = (hashCode * 397) ^ e.GetHashCode();
                hashCode = (hashCode * 397) ^ d.GetHashCode();
                hashCode = (hashCode * 397) ^ f.GetHashCode();
                return hashCode;
            }
        }
    }
    

    You may also see: Correct way to override Equals() and GetHashCode()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way in C# to merge two dictionaries? I have two dictionaries
Here is my problem: I have two dictionaries with identical structures: Dictionary<string, List<Object>> Existing
I have two dictionaries with the same structure: Dictionary<string, int> foo = new Dictionary<string,
I have two complex dictionaries in the form Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> So as
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
I have two dictionaries with key-value pairs as follows: dict-1 ch:23, 100 ch:24, 95
There are two dictionaries like this, two dictionaries have same keys but different values.
I have a main function that contains two dictionaries that I would like to
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
I have two python dictionaries containing information about japanese words and characters: vocabDic :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.