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

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 1396
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
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
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
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
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
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
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
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
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
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
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
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,...
0
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...
0
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...

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.