473,386 Members | 1,801 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,386 software developers and data experts.

System.Collections.Hashtable.Add question


Hello,

I cannot seem to find this in the MSDN documentation: what I would
like to ask is whether the System.Collections.Hashtable.Add should
throw an exception or replace the old value when an element with
the same key value as an existing one is added. For instance, is
the following code legal in C#?

hashtable.Add("aaa", "foo");
hashtable.Add("aaa", "bar");

Thanks,

Good night,

Neil

Nov 16 '05 #1
3 8015
Why don't you write it and find it for yourself?

-vJ

<nz******@gmail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...

Hello,

I cannot seem to find this in the MSDN documentation: what I would
like to ask is whether the System.Collections.Hashtable.Add should
throw an exception or replace the old value when an element with
the same key value as an existing one is added. For instance, is
the following code legal in C#?

hashtable.Add("aaa", "foo");
hashtable.Add("aaa", "bar");

Thanks,

Good night,

Neil

Nov 16 '05 #2
An exception will be thrown. The type will be ArgumentException.

This info is available in the MSDN documentation.

<nz******@gmail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...

Hello,

I cannot seem to find this in the MSDN documentation: what I would
like to ask is whether the System.Collections.Hashtable.Add should
throw an exception or replace the old value when an element with
the same key value as an existing one is added. For instance, is
the following code legal in C#?

hashtable.Add("aaa", "foo");
hashtable.Add("aaa", "bar");

Thanks,

Good night,

Neil

Nov 16 '05 #3
<nz******@gmail.com> wrote:
I cannot seem to find this in the MSDN documentation: what I would
like to ask is whether the System.Collections.Hashtable.Add should
throw an exception or replace the old value when an element with
the same key value as an existing one is added. For instance, is
the following code legal in C#?

hashtable.Add("aaa", "foo");
hashtable.Add("aaa", "bar");


From the docs for Add:

<quote>
The Item property can also be used to add new elements by setting the
value of a key that does not exist in the Hashtable. For example:
myCollection["myNonexistentKey"] = myValue. However, if the specified
key already exists in the Hashtable, setting the Item property
overwrites the old value. In contrast, the Add method does not modify
existing elements.
</quote>

and under Exceptions:

<quote>
ArgumentException An element with the same key already exists
in the Hashtable.
</quote>

So, if you want an exception, use Add. If you don't, use the indexer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

10
by: Mart | last post by:
What class does everyone out there use if they want to store a set of values efficiently? In java I use a HashSet, but there is no equivalent in C#. Even worse, the lowest level interface to...
1
by: Mark | last post by:
Is there an online site where the various methods of storing arrays of classes and variables are explained? For example, I'd love a grid that showed the various pluses and minues of an Array,...
6
by: Chua Wen Ching | last post by:
Hi there, 1) I am looking for the best collections techniques to be used in my program. Is hashtable or arraylist or any .net collection, which is the fastest when adding, getting out data???...
1
by: Sky Sigal | last post by:
Hello: Is there a way to get the path of the current website I am working on during designTime (when current Context == null)? System.Web.HttpRuntime.AppDomainAppPath is not available during...
0
by: Steve Randall | last post by:
I have a class that contains a member variable that is a hashtable. This hashtable holds a number of other class objects and it these other class objects that I want to be able to manage using the...
4
by: aaj | last post by:
Hi all can anyone shed a little light on to my Newbie problem? I have written my own collection that impliements the IEnumerator/Enumerable interfaces and on the surface, all seems well. ...
4
by: Slonocode | last post by:
I have a hashtable full of objects. There seems to be no way to get an object from the hashtable and modify it. Are collections or hashtables in particular not meant for this kind of operation?
1
by: Ivan | last post by:
is it possible to make a universal hashtable that would be able to serialize itself to xml format? universal one, meaning I don't have to know about the type of keys and values in advance. Ivan
0
by: Simon Hart | last post by:
Hi, I have the following code which is miss-behaving at one site: public class HMRHost { private int _pid;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.