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 1096547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:17:58+00:00 2026-05-17T00:17:58+00:00

I was playing around with Dictionary and stumbled across the below scenario public class

  • 0

I was playing around with Dictionary and stumbled across the below scenario

public class MyObject
{
    public string I { get; set; }
    public string J { get; set; }
    public string K { get; set; }

    public override int GetHashCode()
    {
        int hashCode = (I+J+K).GetHashCode();
        Debugger.Log(9, "INFO", hashCode.ToString() + System.Environment.NewLine);
        return hashCode;
    }
}
class Program
{
    static void Main(string[] args)
    {
        MyObject obj1 = new MyObject() { I = "Hello", J = "World" };
        MyObject obj2 = new MyObject() { I = "Hello", J = "World" };

        Dictionary<MyObject, string> collection = new Dictionary<MyObject, string>();
        collection.Add(obj1, "1");
        var result = collection[obj2]; // KeyNotFound exception here.
    }
}

I have MyObject class which acts as a key to dictionary and I override the GetHashCode method to return hash code based on the values stored in the class.

So, when the above code is executed, both obj1 and obj2 returns same hash code, but still the dictionary throws KeyNotFound exception.

Any reason why such a behavior?

  • 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-17T00:17:58+00:00Added an answer on May 17, 2026 at 12:17 am

    In .NET, GetHashCode is used in concert with the Equals method to determine object equality with regard to storage in collections.

    Note that a hash table is more complex than simply mapping a key to a single slot via a hash code. Due to the nature of hash codes, collisions can occur and do occur in practice (though with a good hash function this should not be very often). Thus most hash table implementations have to deal with the case of two different objects generating the same hash code and this is often achieved with a linked list at each “slot” in the hash table. The hash code is used to determine the slot and the Equals method is used to determine whereabouts the object is stored in the linked list (in most “standard” implementations of a hash table).

    A word of warning, however: there are very few good reasons to override the built-in behaviour of GetHashCode. I found this interesting SO thread discussing GetHashCode and Equals that should be worth a read: Why is it important to override GetHashCode when Equals method is overridden?. It discusses the merit/demerits of changing the behaviour, the properties of good and bad hash functions, the required properties of these two methods and other goodies.

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

Sidebar

Related Questions

been playing around with ResolveClientUrl(~/Confirmation.aspx) and other methods.. I am tryin go get the
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Was playing around a little bit with the TimeSpan class and I started wondering
Im playing around with the Tablesorter plug-in for jQuery and was trying to get
I was just playing around with ckeditor and can't get the darn thing to
While playing around with one-to-one associations in castle activerecord I stumbled upon the following
I've been playing around with Simple.Data and have run across something that I can't
I'm playing around with some code katas and trying to get a better understanding
been playing around with Capistrano to get an automated deploy between my server and
Playing around with type-classes I came up with the seemingly innocent class Pair p

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.