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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:19:55+00:00 2026-05-18T08:19:55+00:00

I know that when I create a dictionary with a custom class as key,

  • 0

I know that when I create a dictionary with a custom class as key, the match when I provide a key is done through reference compare. For example:

public class SomeClass
{
    public object SomeValue { get; set; }
}

// ....
public static void Main()
{
    var dict = new Dictionary<SomeClass, string>();

    var key1 = new SomeClass { SomeValue = 30 };
    dict[key1] = "30";

    Console.WriteLine(dict[key1]); // prints "30"

    var key2 = new SomeClass { SomeValue = 30 };
    Console.WriteLine(dict[key2]); // prints null 
}

What happens if I override Equals (and ==) in the SomeClass class? Will I get “30” on the second line of the output?

And what if I want to have a dictionary that is based on references instead of member values, but I have overridden Equals?

Thanks!!

  • 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-18T08:19:56+00:00Added an answer on May 18, 2026 at 8:19 am

    Short Answer

    Yes if you override Equals and GetHashCode methods your custom key comparison will start working.

    Long Answer

    The Dictionary<TKey,TValue> class does not necessarily do a reference based comparison. It instead uses the IEqualityComparer<TKey> instance which can be provided to the constructor. If not provided the default is EqualityComparer<T>.Default.

    The processes by which EqualityComparer<T>.Default works is complicated. But a summary is

    • Looks for IEquatable<T> on the type and if present it is used for equality
    • Defaults to using the Equals method which by default is Object.Equals and hence a reference comparison

    So types can override the the comparison at a couple of levels

    • By specifying a custom IEqualityComparer<T>
    • Implementing IEquatable<T> and overriding GetHashCode
    • Overriding Equals and GetHashCode

    The equality operators == and != do not come into play for the TKey type in a Dictionary<TKey,TValue>.

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

Sidebar

Related Questions

i know that if you create a foreign key on a field (parent_id) in
I'm using python 3.1.1. I know that I can create byte objects using the
I know that we should only create repositories for Aggregate Roots and not for
I know that horizontal partitioning...you can create many tables. How can you do this
I know that it is possible to create .xls files from Delphi using ADO,
I know that in a standalone application I create one of the application context
I know that if i have rootViewController that start with tabBar i can create
I know that for regular path I can use the MAX_PATH constant to create
So I know that it's possible to create scripts for use on spreadsheets using
I know that we can't overload operator with other meaning, we can't create new

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.