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

  • SEARCH
  • Home
  • 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 5950217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:21:44+00:00 2026-05-22T17:21:44+00:00

I need to use Dictionary<long, string> collections that given two instances d1 and d2

  • 0

I need to use Dictionary<long, string> collections that given two instances d1 and d2 where they each have the same KeyValuePair<long, string> contents, which could be inserted in any order:

  1. (d1 == d2) evaluates to true
  2. d1.GetHashCode() == d2.GetHashCode()

The first requirement was achieved most easily by using a SortedDictionary instead of a regular Dictionary.

The second requirement is necessary because I have one point where I need to store Dictionary<Dictionary<long, string>, List<string> – the main Dictionary type is used as the key for another Dictionary, and if the HashCodes don’t evaluate based on identical contents, the using ContainsKey() will not work the way that I want (ie: if there is already an item inserted into the dictionary with d1 as its key, then dictionary.ContainsKey(d2) should evaluate to true.

To achieve this, I have created a new object class ComparableDictionary : SortedDictionary<long, string>, and have included the following:

public override int GetHashCode() {            
   StringBuilder str = new StringBuilder();
   foreach (var item in this) {
      str.Append(item.Key);
      str.Append("_");
      str.Append(item.Value);
      str.Append("%%");
   }
   return str.ToString().GetHashCode();
 }

In my unit testing, this meets the criteria for both equality and hashcodes. However, in reading Guidelines and Rules for GetHashCode, I came across the following:

Rule: the integer returned by GetHashCode must never change while the object is contained in a data structure that depends on the hash code remaining stable

It is permissible, though dangerous, to make an object whose hash code value can mutate as the fields of the object mutate. If you have such an object and you put it in a hash table then the code which mutates the object and the code which maintains the hash table are required to have some agreed-upon protocol that ensures that the object is not mutated while it is in the hash table. What that protocol looks like is up to you.

If an object’s hash code can mutate while it is in the hash table then clearly the Contains method stops working. You put the object in bucket #5, you mutate it, and when you ask the set whether it contains the mutated object, it looks in bucket #74 and doesn’t find it.

Remember, objects can be put into hash tables in ways that you didn’t expect. A lot of the LINQ sequence operators use hash tables internally. Don’t go dangerously mutating objects while enumerating a LINQ query that returns them!

Now, the Dictionary<ComparableDictionary, List<String>> is used only once in code, in a place where the contents of all ComparableDictionary collections should be set. Thus, according to these guidelines, I think that it would be acceptable to override GetHashCode as I have done (basing it completely on the contents of the dictionary).

After that introduction my questions are:

  1. I know that the performance of SortedDictionary is very poor compared to Dictionary (and I can have hundreds of object instantiations). The only reason for using SortedDictionary is so that I can have the equality comparison work based on the contents of the dictionary, regardless of order of insertion. Is there a better way to achieve this equality requirement without having to use a SortedDictionary?
  2. Is my implementation of GetHashCode acceptable based on the requirements? Even though it is based on mutable contents, I don’t think that that should pose any risk, since the only place where it is using (I think) is after the contents have been set.

Note: while I have been setting these up using Dictionary or SortedDictionary, I am not wedded to these collection types. The main need is a collection that can store pairs of values, and meet the equality and hashing requirements defined out above.

  • 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-05-22T17:21:45+00:00Added an answer on May 22, 2026 at 5:21 pm

    Your GetHashCode implementation looks acceptable to me, but it’s not how I’d do it.

    This is what I’d do:

    • Use composition rather than inheritance. Aside from anything else, inheritance gets odd in terms of equality
    • Use a Dictionary<TKey, TValue> variable inside the dictionary
    • Implement GetHashCode by taking an XOR of the individual key/value pair hash codes
    • Implement equality by checking whether the sizes are the same, then checking every key in “this” to see if its value is the same in the other dictionary.

    So something like this:

    public sealed class EquatableDictionary<TKey, TValue>
        : IDictionary<TKey, TValue>, IEquatable<ComparableDictionary<TKey, TValue>>
    {
        private readonly Dictionary<TKey, TValue> dictionary;
    
        public override bool Equals(object other)
        {
            return Equals(other as ComparableDictionary<TKey, TValue>);
        }
    
        public bool Equals(ComparableDictionary<TKey, TValue> other)
        {
            if (ReferenceEquals(other, null))
            {
                return false;
            }
            if (Count != other.Count)
            {
                return false;
            }
            foreach (var pair in this)
            {
                var otherValue;
                if (!other.TryGetValue(pair.Key, out otherValue))
                {
                    return false;
                }
                if (!EqualityComparer<TValue>.Default.Equals(pair.Value,
                                                             otherValue))
                {
                    return false;
                }
            }
            return true;
        }
    
        public override int GetHashCode()
        {
            int hash = 0;
            foreach (var pair in this)
            {
                int miniHash = 17;
                miniHash = miniHash * 31 + 
                       EqualityComparer<TKey>.Default.GetHashCode(pair.Key);
                miniHash = miniHash * 31 + 
                       EqualityComparer<Value>.Default.GetHashCode(pair.Value);
                hash ^= miniHash;
            }
            return hash;
        }
    
        // Implementation of IDictionary<,> which just delegates to the dictionary
    }
    

    Also note that I can’t remember whether EqualityComparer<T>.Default.GetHashCode copes with null values – I have a suspicion that it does, returning 0 for null. Worth checking though 🙂

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

Sidebar

Related Questions

I need to create a multi-dimensional (nested) hashtable/dictionary so that I can use syntax
I need a java dictionary that would be like (C# representation): Dictionary<SomeEnumType, List<int, long>>
I have a Dictionary<string,int> that has the potential to contain upwards of 10+ million
I have a simple 3 column csv file that i need to use python
Need to use own imaged markers instead built-in pins. I have several questions. 1.
I use Dictionary as a heterogeneous container: Dictionary<string, Object> _Table; It basically maps some
using: VS2008, C# I have a COM dll I need to use in a
I need to use a big data structure, more specifically, a big dictionary to
I have a Javascipt object which I use as dictionary var obj={ xxx:'1' yyy:'2'
public static Dictionary<int, string> dic = new Dictionary<int, string>() { {1,anystring1}, {2,anystring2}}; I need

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.