Comparing Hashtables

Posted on 2003-10-23
Last Modified: 2012-05-04
Does anyone know how to compare two hashtables?

Hashtable ht1 = new Hashtable();
ht1.Add( "exampleKey", "exampleValue" );

Hashtable ht2 = new Hashtable();
ht2.Add( "exampleKey", "exampleValue" );

// This returns false
ht1.Equals( ht2 );
Question by:chadboyer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 11

Expert Comment

ID: 9609713
it depends on what you wanna compare.
if you wann check if they are euqal you can check if the dimensions are the same at first.
if the dimensions are the same, you can doublecheck the specific elements to each other.
do you mean that?

Author Comment

ID: 9609873
I want to compare the dimensions and the elements.  I was just hoping that ht1.Equals( ht2 ) would do the work for me.  But it doesn't.
LVL 22

Accepted Solution

_TAD_ earned 125 total points
ID: 9609957

ht1.Equals(ht2) checks to see if the ht1 and ht2 variables refer to the same object.

Hastable ht1 = new Hashtabl();
..    Fill Hashtable

Hashtable ht2 = ht1;

ht1.Equals(ht2)   //  TRUE

This is because ht1 and ht2 really are the same object

Hastable ht1 = new Hashtabl();
Hastable ht2 = new Hashtabl();
ht1.Equals(ht2) // FALSE

you have two distinct objects here ( two "new" statements means two unique obects)


Author Comment

ID: 9610030
I wrote my own Hashtable compare function.  Thanks anyway...
LVL 22

Expert Comment

ID: 9610174

here is some code to compare hashtable values and keys

                  Hashtable ht1 = new Hashtable();


                  Hashtable ht2 = new Hashtable();


                  IDictionaryEnumerator myEnum = ht1.GetEnumerator();
                  bool isSame = true;

                  while ( myEnum.MoveNext() )
                        if (ht2.ContainsKey(myEnum.Key))
                              if (ht1[myEnum.Key]!=ht2[myEnum.Key])
                                    isSame = false;
                              isSame = false;

                  Console.WriteLine("Hashtables ht1 & ht2 are {0}the same", (isSame)?"":"NOT ");

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question