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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:39:33+00:00 2026-05-15T12:39:33+00:00

Say I have some special class, WrappedDataTable , and I want to associate each

  • 0

Say I have some special class, WrappedDataTable, and I want to associate each WrappedDataTable with exactly one DataTable. Furthermore, I want there to be no more than one WrappedDataTable in existence for any given DataTable.

A colleague suggested I could cache my WrappedDataTable and use a factory method to access one, like this:

public static class DataTableWrapper
{
    private Dictionary<DataTable, WrappedDataTable> _wrappedTables;

    static DataTableWrapper()
    {
        _wrappedTables = new Dictionary<DataTable, WrappedDataTable>();
    }

    public static WrappedDataTable Wrap(this DataTable table)
    {
        WrappedDataTable wrappedTable;
        if (!_wrappedTables.TryGetValue(table, out wrappedTable))
            _wrappedTables[table] = wrappedTable = new WrappedDataTable(table);

        return wrappedTable;
    }
}

This struck me as very questionable at first, I guess because I’ve become familiar with the idea that keys in a dictionary should be immutable types. But perhaps this is not necessarily the case? A quick test revealed to me that a DataTable appears to maintain a consistent hash code over the course of numerous modifications to its contents; a Dictionary<DataTable, TValue> therefore appears to be able to return a correct value for ContainsKey consistently.

What I’m wondering is if the base version of object.GetHashCode by default will return an unchanging value for every individual object, or if what I’m seeing with DataTable is just an illusion?

If the former is true — and object.GetHashCode works just fine — it seems the “use only immutable types as keys” advice really only applies to scenarios where:

  1. You want equality of objects to be about value equality as opposed to reference equality, and/or:
  2. You have a custom type with its own GetHashCode implementation that is based on the type’s members.

Any sages out there care to shed some light on this for me?


UPDATE: Thanks to Jon Skeet for answering my question. In other news, I did some digging and think I came up with an IEqualityComparer<T> that does provide identity comparison after all! Check it out (sorry VB.NET haters, I just had a VB.NET project up so that’s what I wrote it in — translation is trivial):

Imports System.Collections.Generic
Imports System.Runtime.CompilerServices

Public Class IdentityComparer(Of T As Class)
    Implements IEqualityComparer(Of T)

    Public Overloads Function Equals(ByVal x As T, ByVal y As T) As Boolean _
        Implements IEqualityComparer(Of T).Equals

        Return Object.ReferenceEquals(x, y)
    End Function

    Public Overloads Function GetHashCode(ByVal obj As T) As Integer _
        Implements IEqualityComparer(Of T).GetHashCode

        Return RuntimeHelpers.GetHashCode(obj)
    End Function
End Class

Take a look at this example program:

Dim comparer As IEqualityComparer(Of String) = New IdentityComparer(Of String)

Dim x As New String("Hello there")
Dim y As New String("Hello there")

Console.WriteLine(comparer.Equals(x, y))
Console.WriteLine(comparer.GetHashCode(x))
Console.WriteLine(comparer.GetHashCode(y))

Output:

False
37121646
45592480
  • 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-15T12:39:33+00:00Added an answer on May 15, 2026 at 12:39 pm

    It doesn’t have to return a unique value. It just has to return an unchanging one – and that’s what object.GetHashCode does.

    So long as DataTable doesn’t override Equals or GetHashCode, you’ve basically got object identity as equality – which means it doesn’t matter if the object is mutated.

    Personally I’d like to see an implementation of IEqualityComparer<T> which provides identity equality for any type, but we can’t implement that ourselves – there’s no way of finding out what GetHashCode would have returned if it hadn’t been overridden. (Java has this capability in its standard libraries, but .NET doesn’t. Grr.)

    EDIT: Woot – with object.ReferenceEquals and RuntimeHelpers.GetHashCode(), we can easily implement an IdentityEqualityComparer<T>. Yay!

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

Sidebar

Related Questions

Say I have some code like namespace Portal { public class Author { public
Let's say I have some Java code: public class SomeClass { static { private
Google is failing me on this one. Let's say I have some ECMA script
Say I have some windows method and a struct: struct SomeStruct{ int foo; int
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
Let's say I have some pointers called: char * pChar; int * pInt; I
Let's say I have some code like this if(isset($_GET['foo'])) //do something if(isset($_GET['bar'])) //do something
Let's say I have some XML like this <channel> <item> <title>This is title 1</title>
I'm not even sure if it's possible but say I have some XML: <source>
Let's say I have committed some bad changes to Subversion repository. Then I commit

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.