What is the difference between Hashtable and NameValueCollection? Thanks.

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

with a hashtable you can set an existing value using its indexer:

myTable["mykey"] = newValue;
 
Hi,

The difference between Hashtable and NameValueCollection are the following,

1) As you have mentioned in your post, the NameValueCollection can contain more than one values for a particular key whereas Hashtable can contain only one value for a particular key.
2) The Hashtable collection is much more faster than NameValueCollection, since it uses the Hashtable algorithm which is faster for retrieves.

For the other part of your question about how to update an item in the hashtable,
You can use the Item property to update the value of the key. But you have to note that, if the specified key already exists in the Hashtable, the value is replaced; otherwise, a new element is created.

I guess it is good practice to check whether the key exists in the collection using Contains or ContainsKey method before you try to update.

I hope this helps...

Regards,
Madhu

Microsoft C# MVP | MCSD.NET
 
It's also worth mentioning that a NameValueCollection can only contain
strings and has this weird interface where it returns multiple values for
one key as a comma separated string.

Madhu said:
Hi,

The difference between Hashtable and NameValueCollection are the following,

1) As you have mentioned in your post, the NameValueCollection can contain
more than one values for a particular key whereas Hashtable can contain only
one value for a particular key.
2) The Hashtable collection is much more faster than NameValueCollection,
since it uses the Hashtable algorithm which is faster for retrieves.
For the other part of your question about how to update an item in the hashtable,
You can use the Item property to update the value of the key. But you have
to note that, if the specified key already exists in the Hashtable, the
value is replaced; otherwise, a new element is created.
I guess it is good practice to check whether the key exists in the
collection using Contains or ContainsKey method before you try to update.
 
You can use the Item property to update the value of the key. But you have
to note that, if the specified key already exists in the > Hashtable, the
value is replaced; otherwise, a new element is created.

This is not exactly true. If capacity of hash table is set, simple item[key]
= will add element only when there is free slot. Otherwise assignment will
be "ignored" and you have to use hashTable.Add method to add new value.

HTH
Alex
 
davidw said:
I always use NameValueCollection. But I read an article says the only
differece between Hashtable and NameValueCollection is that
NameValueCollection could accept more than one value with same key? I am not
quite understand this.

Well, it's not quite the only difference. Hashtables can take any
object as the key and value; NameValueCollections only work with
strings. Essentially NameValueCollection has a list of strings for each
key; a Hashtable only has a single value for each key.
And I think NameValueCollection's Set is a nice
method. If I use HashTable, how can I update a item in it? Do I have to
check if it exists, and remove and add it again?

No, you can just do

myHashTable[key] = value;
 
AlexS said:
You can use the Item property to update the value of the key. But you have
to note that, if the specified key already exists in the > Hashtable, the
value is replaced; otherwise, a new element is created.

This is not exactly true. If capacity of hash table is set, simple item[key]
= will add element only when there is free slot. Otherwise assignment will
be "ignored" and you have to use hashTable.Add method to add new value.

Could you give an example of that? It seems unlikely to me. Here's a
sample which suggests otherwise:

using System;
using System.Collections;

class Test
{
static void Main()
{
Hashtable table = new Hashtable(5);

for (int i=0; i < 10000; i++)
{
table = i;
}
Console.WriteLine (table.Count);
}
}
 
I always use NameValueCollection. But I read an article says the only
differece between Hashtable and NameValueCollection is that
NameValueCollection could accept more than one value with same key? I am not
quite understand this. And I think NameValueCollection's Set is a nice
method. If I use HashTable, how can I update a item in it? Do I have to
check if it exists, and remove and add it again?


Thanks.
 
capacity will be updated when the load factor of the hashtable is reached.
assignment won't be ignored.

--
Regards,
Hin

AlexS said:
You can use the Item property to update the value of the key. But you have
to note that, if the specified key already exists in the > Hashtable, the
value is replaced; otherwise, a new element is created.

This is not exactly true. If capacity of hash table is set, simple item[key]
= will add element only when there is free slot. Otherwise assignment will
be "ignored" and you have to use hashTable.Add method to add new value.

HTH
Alex
 
Back
Top