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

The Archive Base Latest Questions

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

Is it possible to use an object as a key for a Dictonary<object, …>

  • 0

Is it possible to use an object as a key for a Dictonary<object, ...> in such a way that the Dictionary treats objects as equal only if they are identical?

For example, in the code below, I want Line 2 to return 11 instead of 12:

Dictionary<object, int> dict = new Dictionary<object, int>();
object a = new Uri("http://www.google.com");
object b = new Uri("http://www.google.com");

dict[a] = 11;
dict[b] = 12;

Console.WriteLine(a == b);  // Line 1. Returns False, because a and b are different objects.
Console.WriteLine(dict[a]); // Line 2. Returns 12
Console.WriteLine(dict[b]); // Line 3. Returns 12

The current Dictionary implementation uses object.Equals() and object.GetHashCode() on the keys; but I am looking for a different kind of dictionary that uses the object’s identity as a key (instead of the object’s value). Is there such a Dictionary in .NET or do I have to implement it from scratch?

  • 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-28T08:10:20+00:00Added an answer on May 28, 2026 at 8:10 am

    You don’t need to build your own dictionary – you need to build your own implementation of IEqualityComparer<T> which uses identity for both hashing and equality. I don’t think such a thing exists in the framework, but it’s easy enough to build due to RuntimeHelpers.GetHashCode.

    public sealed class IdentityEqualityComparer<T> : IEqualityComparer<T>
        where T : class
    {
        public int GetHashCode(T value)
        {
            return RuntimeHelpers.GetHashCode(value);
        }
    
        public bool Equals(T left, T right)
        {
            return left == right; // Reference identity comparison
        }
    }
    

    I’ve restricted T to be a reference type so that you’ll end up with objects in the dictionary; if you used this for value types you could get some odd results. (I don’t know offhand how that would work; I suspect it wouldn’t.)

    With that in place, the rest is easy. For example:

    Dictionary<string, int> identityDictionary =
        new Dictionary<string, int>(new IdentityEqualityComparer<string>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a generic dictionary of objects where they key is a
Is it possible to use .slideUp, but the object only slideUp by (For instance)
I was trying to use a CLLocation object as a key for a dictionary...
I have a Dictionary object in my ViewModel with key/values that translate words on
Is it possible to use an object as a hash key? For example, the
Is it possible to use an ActiveX/COM object from ColdFusion? If so, where's the
Is it possible to use PHP's SimpleXML functions to create an XML object from
Is it possible to use the __unused attribute macro on Objective-C object method parameters?
Is it somehow possible to use the 'With' keyword on an existing object? I
Is it possible to use the .NET configuration objects to get at .config files

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.