C
cody
with a hashtable you can set an existing value using its indexer:
myTable["mykey"] = newValue;
myTable["mykey"] = newValue;
more than one values for a particular key whereas Hashtable can contain onlyMadhu said:Hi,
The difference between Hashtable and NameValueCollection are the following,
1) As you have mentioned in your post, the NameValueCollection can contain
since it uses the Hashtable algorithm which is faster for retrieves.2) The Hashtable collection is much more faster than NameValueCollection,
to note that, if the specified key already exists in the Hashtable, theFor 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
collection using Contains or ContainsKey method before you try to update.I guess it is good practice to check whether the key exists in the
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.
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?
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.
AlexS said:to note that, if the specified key already exists in the > Hashtable, theYou can use the Item property to update the value of the key. But you have
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