473,498 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a structure as a hashtable key

I am wondering if there is any problems with using a structure (contains 2
strings and 1 datetime) as a key to a hashtable. I have created one without
overriding the gethashcode or equals functions and it seems to work. However
i am not sure if occasionally this can cause a problem with uniqueness. The
examples i have seen all use classes and override the two functions mentioned
above. I may make the change from structure to class, but i want to be sure
i am making a gain by doing so. Thanks for any information on this.

Oct 21 '05 #1
4 1397
I am wondering if there is any problems with using a structure (contains 2
strings and 1 datetime) as a key to a hashtable.
Assuming you're using the non-generic Hashtable class, it will cause a
lot of unnecessary boxing.

I have created one without
overriding the gethashcode or equals functions and it seems to work.


You should definitely override those and provide meaningful
implementations for types used as hash keys.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 21 '05 #2
Also, the object.GetHashCode is not guaranteed to be unique so you can run
into problems (that might be hard to debug) when a duplicate key is
generated.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...
I am wondering if there is any problems with using a structure (contains 2
strings and 1 datetime) as a key to a hashtable.


Assuming you're using the non-generic Hashtable class, it will cause a
lot of unnecessary boxing.

I have created one without
overriding the gethashcode or equals functions and it seems to work.


You should definitely override those and provide meaningful
implementations for types used as hash keys.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Oct 21 '05 #3
Ok. Sounds like the better idea (more complete solution) is to use a class
intead of a structure and override the GetHashCode and Equals functions.
Thanks for the advise.

"Peter Rilling" wrote:
Also, the object.GetHashCode is not guaranteed to be unique so you can run
into problems (that might be hard to debug) when a duplicate key is
generated.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...
I am wondering if there is any problems with using a structure (contains 2
strings and 1 datetime) as a key to a hashtable.


Assuming you're using the non-generic Hashtable class, it will cause a
lot of unnecessary boxing.

I have created one without
overriding the gethashcode or equals functions and it seems to work.


You should definitely override those and provide meaningful
implementations for types used as hash keys.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Oct 22 '05 #4
Peter Rilling <pe***@nospam.rilling.net> wrote:
Also, the object.GetHashCode is not guaranteed to be unique so you can run
into problems (that might be hard to debug) when a duplicate key is
generated.


GetHashCode never has to be unique - and indeed if you have more than
one integer in your data, for instance, you *can't* make it unique.

So long as it returns the same value for equal objects, and so long as
Equals itself is correct, you shouldn't have problems.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '05 #5

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

Similar topics

5
332
by: Bob Weiner | last post by:
I'm new to C#. What is the best (in terms of programmability and use) data structure to use to maintain an array of information with containing 3 fields of differing types. For instance:...
6
7086
by: MrNobody | last post by:
When I initialize my program I need to load from several sources of data from files which are in a table form- in each set of data they share fields (like say name address phone). I used to load...
7
3114
by: Leszek Taratuta | last post by:
Hello, I need a kind of lightweight data structure known as "associative array". It will store a few values that I need to access using textual keys. The Hashtable is too heavy for me. I also...
3
8871
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
4
258
by: C Villalba | last post by:
I am wondering if there is any problems with using a structure (contains 2 strings and 1 datetime) as a key to a hashtable. I have created one without overriding the gethashcode or equals...
6
17132
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
6
2605
by: James | last post by:
I am using vb.net and need to keep in memory a large data structure, so I am looking for the best option. And after several test I am pretty confused. So I will be grateful if anyone can help me. ...
22
2020
by: Daniel Rucareanu | last post by:
I have the following script: function Test(){} Test.F = function(){} Test.F.FF = function(){} Test.F.FF.FFF = function(){} Test.F.FF.FFF.FFFF = function(){} //var alias = function(){}; var...
7
1941
by: Mike P | last post by:
I have 2 hashtables each of which I am using to store a set of IDs and Descriptions. Hashtable 1 will have the full set of data (e.g. IDs 1,2,3,4,5) whereas Hashtable 2 will only have a subset of...
0
7125
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,...
0
7002
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...
0
7165
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,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
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...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.