473,322 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

What is the difference between Hashtable and NameValueCollection? Thanks.

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.
Nov 16 '05 #1
7 14666
with a hashtable you can set an existing value using its indexer:

myTable["mykey"] = newValue;

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"davidw" <da****@affinisys.com> schrieb im Newsbeitrag
news:eC**************@TK2MSFTNGP10.phx.gbl...
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.

Nov 16 '05 #2
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
"davidw" wrote:
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.

Nov 16 '05 #3
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[C#-MVP]" <Ma*******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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
"davidw" wrote:
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.

Nov 16 '05 #4
> 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

Nov 16 '05 #5
davidw <da****@affinisys.com> wrote:
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;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
AlexS <sa***********@SPAMsympaticoPLEASE.ca> wrote:
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] = i;
}
Console.WriteLine (table.Count);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Hin
capacity will be updated when the load factor of the hashtable is reached.
assignment won't be ignored.

--
Regards,
Hin

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:u9****************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: oliver.wulff | last post by:
Does there exist a class similar to a hashtable which provides a key-value pair collection. The key and the value has to be a string?
2
by: John Grandy | last post by:
What are good rules of thumb to use when deciding between a NameValueCollection, an ArrayList, or a Hashtable ?
3
by: Mike Logan | last post by:
We are trying to serialize the Request.ServerVariables collection (NameValueCollection) to an XML formatted string, to insert into a database. The problem we are running into is that most of the...
5
by: ari | last post by:
hey all, i was wondering what the difference is between obtaining a user name like Request.ServerVariables("Auth_User").Split("\")(1)) and User.Identity.Name
2
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
2
by: MrDotNet | last post by:
Hi I want pass NameValueCollection as parameter in webmethod. I try it but that give me error. Here is Error. You must implement the Add(System.String) method on...
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
17
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
4
by: Water Cooler v2 | last post by:
I can't believe I've stumbled on a simple problem such as this. After all these years, that too. Anyway, it goes that I just realized that I could not iterate through my hashtable and print out...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.