473,569 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When Do You Need To Implement "GetHashCod e?"

Dad
Hi,

A number of interfaces, e.g., IEqualityCompar er, include GetHashCode.
When does it need to be implemented? I usually just return 0 or 1.

Thanks,
Gary

Mar 10 '08 #1
1 1968
In short, any time you expect to use hash-logic on the item - for
example, use it as the key in a lookup.

GetHashCode() is used as a mechanism for doing a hash-match equality
test, in particular for building hash-tables, such as in
Dictionary<TKey , TValueetc. In this case, the hash-code (or a
modulo) is used to split the items into different buckets; it is
further used to to hash-match equality tests.

The rules are that if the hash match of 2 items is different they
*can't* be the same; if it is the same, then check the actual equality
test (which mihgt be more expensive).

By returning a constant value, you are a: forcing everything into the
same bucket, and b: forcing the system to check every item for
equality, rather than being able to just look at those with matching
hash values. Ideally, the hash-code would be generated from something
immutable, or which you *expect* to be immutable while the item is
used as a key. Otherwise chaos breaks out, as it can fail to find the
item you know is there...

If none of that makes sense, post back...

Marc
Mar 10 '08 #2

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

Similar topics

7
4788
by: | last post by:
Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our term to dump Borland (not only BCB). As a part of my attempt to dump long-loved BCB I'm trying to investigate how one can implement "__property" in ANSI C++. Would anyone have a solution hot to implement "__property" in ANSI C++ ie. how to make following code compile...
3
11621
by: babylon | last post by:
any facilities in csharp that can help me implmenting undo/redo in my application? thx
1
2613
by: jiatiejun | last post by:
I want to write a file tools that can access files by "send to " or "Open with " in Explorer shell but I don't know how to implement who can help me thanks
1
1773
by: Jeff Brown | last post by:
Hi, I'm trying to implement the functionality of the generic expression, "default(T)", but for a given instance of the Type class. The ideal would be if the Type class had a method like "object Type.MakeDefaultInstance()" -- but it doesn't. Here's my attempt, but it seems rather kludgy. Is there a better way?
4
2716
by: hagaihe | last post by:
Hello, I've written a Windows service and i need help to implement the meaning of the sentence which is generated automatically by the IDE // More than one user Service may run within the same process. To add // another service to this process, change the following line to // create a second service object. For example, // // ...
6
4796
by: Smithers | last post by:
Just looking to compile a list of "all the ways to implement events". I'm NOT looking to get into the merits or mechanics of each in this thread... just want to identify them all - good, bad, and ugly. Here's what I have so far 1. Implement via the 'event' keyword (with associated delegate, etc) 2. Expose a private delegate via a public...
0
291
by: Owen Zhang | last post by:
What is the best way to implement "tail -f" in C or C++ and higher performance compared to either unix shell command "tail -f" or perl File::Tail ? Any suggestion appreciated. Thanks.
1
1578
by: mohini2000 | last post by:
Hi I want to implement a "Remember Me" feature in a website that basically keeps users logged into the website, even after they've closed the browser so that the next time they come, they won't have to login again manually. If anyone can tell me that how this feature can be implemented. Thanks
11
1881
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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...
0
6286
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...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
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
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.