Click here to Skip to main content
12,694,842 members (33,170 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#2.0 C#
Hi Friends,
I have a Hashtable and I want to compare the values stored in it.
Hastable ht= new Hashtable();

and it has collection as

Key                            Value
Dropdown   --                  1000
Dropdown   --                  2000
Dropdwon   --                  3000

I want to compare the values in Hashtabale
if they are not same, return false;

How can I get this??

I mean something like this...

Posted 17-Jan-12 22:35pm
Updated 17-Jan-12 23:29pm
SAKryukov 18-Jan-12 3:44am
Why? Do you know what a hashtable is designed for?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

As the generics were introduced as early as in .NET Framework v.2.0, it made all non-generic collection types like Hashtable obsolete. Those types were not marked deprecated in order to avoid breaking compatibility with legacy code, but for new development it makes sense to use only generic types.

Instead of Hashtable, you can use the types from System.Collections.Generic. You have a choice from System.Collections.Generic.Dictionary<TKey, TValue>, System.Collections.Generic.SortedDictionary<TKey, TValue> or System.Collections.Generic.SortedDictionary<TKey, TValue>, please see:[^],[^],[^].

They have identical interface, the difference is the difference balance between redundancy in memory consumption and performance.

What else? Ah, looping… Isn't it easy? They all implement the interface IEnumerable<KeyValuePair<TKey, TValue>> and it means… what? Right, it means that you can use foreach:

foreach (KeyValuePair<TKey, TValue> pair in myCollection) {
    //for example:
    TKey key = pair.Key; //use it the way you want
    TValue value = pair.Value; //use it the way you want

Now, look at my comment to the question. You did not fully describe required comparison. Let's assume you want to check up if all values are unique or not. Now, remember the properties of dictionary. All key are unique, right. So, you can create yes another dictionary and use your values as keys. If you add all values as keys to your dictionary, every value is unique is this set.

If you wanted something else (look, you should really look for ambiguities in your questions!) — not a problem: I explained you how to iterate through all members, so you can implement the rest by yourself.

Comments 18-Jan-12 6:47am
System.Collections.Generic.SortedDictionary<TKey, TValue> or System.Collections.Generic.SortedDictionary<TKey, TValue>

Rate this: bad
Please Sign up or sign in to vote.

Solution 4

If you need to check if any of the hashtable values are equal then you could use the Values.CopyTo method and then work with the array using the ordinary for loop, e.g.
  int[] a = new int[hashtable.Values.Count];
  bool flag = false;
  for (int i = 0; i < a.Length-1; i++)
    for (int j = i + 1; j < a.Length; j++)
      if (a[i] == a[j])
        flag = true;


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 18 Jan 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100