473,802 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the difference between Hashtable and NameValueCollec tion? Thanks.

I always use NameValueCollec tion. But I read an article says the only
differece between Hashtable and NameValueCollec tion is that
NameValueCollec tion could accept more than one value with same key? I am not
quite understand this. And I think NameValueCollec tion'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 14748
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****@affinis ys.com> schrieb im Newsbeitrag
news:eC******** ******@TK2MSFTN GP10.phx.gbl...
I always use NameValueCollec tion. But I read an article says the only
differece between Hashtable and NameValueCollec tion is that
NameValueCollec tion could accept more than one value with same key? I am not quite understand this. And I think NameValueCollec tion'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 NameValueCollec tion are the following,

1) As you have mentioned in your post, the NameValueCollec tion 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 NameValueCollec tion, 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 NameValueCollec tion. But I read an article says the only
differece between Hashtable and NameValueCollec tion is that
NameValueCollec tion could accept more than one value with same key? I am not
quite understand this. And I think NameValueCollec tion'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 NameValueCollec tion 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*******@disc ussions.microso ft.com> wrote in message
news:2A******** *************** ***********@mic rosoft.com...
Hi,

The difference between Hashtable and NameValueCollec tion are the following,
1) As you have mentioned in your post, the NameValueCollec tion 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 NameValueCollec tion, 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 NameValueCollec tion. But I read an article says the only
differece between Hashtable and NameValueCollec tion is that
NameValueCollec tion could accept more than one value with same key? I am not quite understand this. And I think NameValueCollec tion'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****@affinis ys.com> wrote:
I always use NameValueCollec tion. But I read an article says the only
differece between Hashtable and NameValueCollec tion is that
NameValueCollec tion 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; NameValueCollec tions only work with
strings. Essentially NameValueCollec tion has a list of strings for each
key; a Hashtable only has a single value for each key.
And I think NameValueCollec tion'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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
AlexS <sa***********@ SPAMsympaticoPL EASE.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.Collecti ons;

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

for (int i=0; i < 10000; i++)
{
table[i] = i;
}
Console.WriteLi ne (table.Count);
}
}

--
Jon Skeet - <sk***@pobox.co m>
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***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:u9******** ********@TK2MSF TNGP10.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
7419
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
3020
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
10641
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 example that we find discuss writing to a file. Does anyone have a simple function to do this? We would also like to deserialize the string from the database back into a NameValueCollection. -- Mike Logan
5
1166
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
6967
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, GetEnumerator, and RemoveAt) but the way it implements all the other things you need to override seems overkill and counters the advantage of having an extensible base class. For example, the documentation example implementation of Remove: Public...
2
6857
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 System.Collections.Specialized.NameValueCollection because it inherits from ICollection. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in...
14
2394
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 hassel with a multi-dimensional array. Is there such an object in VB.Net? Dim obj As someCollectionObj obj.Add("parmA1", "parmA2", "parmA3") obj.Add("parmB1", "parmB2", "parmB3") ....
17
2093
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
3938
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 its contents. On further probing, I realized that the hashtable didn't implenent IList, which really has the this indexer. And so I couldnt do this: for (int i = 0; i < _myHashTable.Count; i++) Console.WriteLine(_myHashTable.ToString());
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.