Questions tagged [gethashcode]

GetHashCode is method of base Object class of .Net Framework.

GetHashCode is method of base Object class of .Net Framework. A hash code is a numeric value that is used to identify an object during equality testing. It can also serve as an index for an object in a collection. The GetHashCode method is suitable for use in hashing algorithms and data structures such as a hash table.

Important notes

  • Equality of GetHashCode value doesn't necessarily imply equality of objects. The equality must be verified using Equals.
  • Inequality of GetHashCode necessarily means inequality of objects.
344 questions
20
votes
3 answers

Object.GetHashCode

My question may duplicate Default implementation for Object.GetHashCode() but I'm asking again because I didn't understand the accepted answer to that one. To begin with I have three questions about the accepted answer to the previous question,…
ChrisW
  • 54,973
  • 13
  • 116
  • 224
19
votes
1 answer

Persistent hashcode for strings

I want to generate an integer hashcode for strings, that will stay constant forever; i.e. the same string should always result in the same hashcode. The hash does not have to be cryptographically secure, it will not be used for passwords or…
HugoRune
  • 13,157
  • 7
  • 69
  • 144
18
votes
2 answers

Will string.GetHashCode() return negative value?

I tried with batch of random strings, all values I got are positive, but I wondering: Will String.GetHashCode() return negative or 0? Since the return value is int, so I guess it might be, so if it is the case, I have to change my logic. If you…
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
18
votes
4 answers

GetHashCode() gives different results on different servers?

I declared a C# line of code like so int hashcode = "apple".GetHashCode(); On my computer, a computer at work, and a friend's computer, the result was 1657858284. On a development server, the result was 1548091822. Is there a way for me to tell…
John
  • 32,403
  • 80
  • 251
  • 422
17
votes
4 answers

Is it possible to combine hash codes for private members to generate a new hash code?

I have an object for which I want to generate a unique hash (override GetHashCode()) but I want to avoid overflows or something unpredictable. The code should be the result of combining the hash codes of a small collection of strings. The hash codes…
Adrian Hope-Bailie
  • 2,445
  • 1
  • 23
  • 26
16
votes
3 answers

Equals vs GetHashCode when comparing objects

Should we override both Equals and GetHashCode properties when implementing a custom class instances comparison? In the following code I have a collection of classes. The class A is compared by the ID, the class B - by Code. using System; using…
serhio
  • 28,010
  • 62
  • 221
  • 374
16
votes
3 answers

Seeding a pseudo-random number generator in C#

I need a seed for an instance of C#'s Random class, and I read that most people use the current time's ticks counter for this. But that is a 64-bit value and the seed needs to be a 32-bit value. Now I thought that the GetHashCode() method, which…
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
15
votes
1 answer

Two equal IPv6 IPAddress instances return different GetHashCode results

I have two clients that create IPAddress instances from the same byte[] and send it to the server over WCF (using DataContractSerializer). On the server, these IPAddress instances are inserted as keys in a dictionary but for some reason they're…
Sivan Krigsman
  • 311
  • 1
  • 7
15
votes
1 answer

Implementation of Object.GetHashCode()

I'm reading Effective C# and there is a comment about Object.GetHashCode() that I didn't understand: Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object created is assigned a unique object…
Belgi
  • 14,542
  • 22
  • 58
  • 68
15
votes
4 answers

new KeyValuePair(i, j).GetHashCode(); High Rate of Duplicates

In search of a fast composite key for Dictionary I came upon anomaly I cannot understand nor justify. In limited testing Dictionary, string> is significantly slower (200:1) than Dictionary
paparazzo
  • 44,497
  • 23
  • 105
  • 176
14
votes
4 answers

When Should a .NET Class Override Equals()? When Should it Not?

The VS2005 documentation Guidelines for Overloading Equals() and Operator == (C# Programming Guide) states in part Overriding operator == in non-immutable types is not recommended. The newer .NET Framework 4 documentation Guidelines for…
Eric J.
  • 147,927
  • 63
  • 340
  • 553
14
votes
2 answers

GetHashCode and Equals are implemented incorrectly in System.Attribute?

Seeing from Artech's blog and then we had a discussion in the comments. Since that blog is written in Chinese only, I'm taking a brief explanation here. Code to reproduce: [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple =…
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
14
votes
6 answers

Generic IEqualityComparer and GetHashCode

Being somewhat lazy about implementing lots of IEqualityComparers, and given that I couldn't easily edit class implementations of object being compared, I went with the following, meant to be used with Distinct() and Except() extension methods.…
mathieu
  • 30,974
  • 4
  • 64
  • 90
14
votes
5 answers

GetHashCode() with string keys

Hey all, I've been reading up on the best way to implement the GetHashCode() override for objects in .NET, and most answers I run across involve somehow munging numbers together from members that are numeric types to come up with a method. Problem…
King Skippus
  • 3,801
  • 1
  • 24
  • 24
13
votes
1 answer

Is the .Net HashSet uniqueness calculation completely based on Hash Codes?

I was wondering whether the .Net HashSet is based completely on hash codes or whether it uses equality as well? I have a particular class that I may potentially instantiate millions of instances of and there is a reasonable chance that some hash…
RobV
  • 28,022
  • 11
  • 77
  • 119
1 2
3
22 23