Something to note about Hashtables

I was actually using the Dictionary class, but the Keys of a Dictionary is a Hashtable, so it has the same issue.
 
When you try and add two value types of the same type to a Hashtable, it blows up, saying "Item has already been added".
 
e.g.

System.Collections.Hashtable ht = new System.Collections.Hashtable();

ht.Add(new Guid(), "hello");
ht.Add(new Guid(), "hello");
 
 
Reference:
 
 

Advertisements

One thought on “Something to note about Hashtables

  1. Actually this makes sense:   Guid is a value type with a default value of Guid.Empty, so in your sample you’re adding the same value for the key twice.i.e what you did was the same as ht.Add("A", "hello");
    ht.Add("A", "hello");or the same asht.Add(666, "hello");
    ht.Add(666, "hello");

    If you were to go:Hashtable ht = new HashTable();ht.Add(Guid.NewGuid(), "hello");ht.Add(Guid.NewGuid(), "hello");that would work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s