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
6
votes
3 answers

Bug in Array.IStructuralEquatable.GetHashCode?

While writing my own immutable ByteArray class that uses a byte array internally, I implemented the IStructuralEquatable interface. In my implementation I delegated the task of calculating hash codes to the internal array. While testing it, to my…
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
5
votes
5 answers

Storing C# GetHashCode() in DB is Unreliable

Possible Duplicate: How do I create a HashCode in .net (c#) for a string that is safe to store in a database? I'm planning to store hundreds of thousands of URLs in my database. Each row in my UrlInfo table will be immutable with the URL itself…
sisdog
  • 2,649
  • 2
  • 29
  • 49
5
votes
2 answers

GetHashCode Equality

I've wondered about this, so I figure I'll ask it. Most places you'll see use the same semantic logic for overriding Equals as GetHashCode for memberwise equality...however they usually use different implementations: public override bool…
Jeff
  • 35,755
  • 15
  • 108
  • 220
5
votes
2 answers

C# performant alternatives to HashSet and Dictionary that do not use GetHashCode

I'm looking for built-in alternatives of HashSet and Dictionary objects that have better performance than lists but do not use the internal GetHashCode method. I need this because for the class I have written, there is no way of writing a…
Kjara
  • 2,504
  • 15
  • 42
5
votes
2 answers

Generating a good hash code (GetHashCode) for a BitArray

I need to generate a fast hash code in GetHashCode for a BitArray. I have a Dictionary where the keys are BitArrays, and all the BitArrays are of the same length. Does anyone know of a fast way to generate a good hash from a variable number of bits,…
bart
  • 1,100
  • 11
  • 14
5
votes
5 answers

Is it ok to have a GUID private property in a class in order to use it in GetHashCode override?

Is it OK to have a GUID private property in a class in order to use it in GetHashCode override? Something like: public class Voucher : IComparable, IComparable, IEquatable { private Guid? _guid; private Guid Guid { …
shadow
  • 1,883
  • 1
  • 16
  • 24
5
votes
2 answers

Why should the "prime-based" hashcode implementation be used instead of the "naive" one?

I have seen that a prime number implementation of the GetHashCode function is being recommend, for example here. However using the following code (in VB, sorry), it seems as if that implementation gives the same hash density as a "naive" xor…
Wilhelm
  • 1,868
  • 14
  • 21
5
votes
2 answers

How to use Object.GetHashCode() on a type that overrides GetHashCode()

I have a class A that implements IEquatable<>, using its fields (say, A.b and A.c) for implementing/overriding Equals() and overriding GetHashCode(), and everything works fine, 99% of the time. Class A is part of a hierarchy (class B, C) that all…
Jimmy
  • 5,131
  • 9
  • 55
  • 81
5
votes
1 answer

Continuing confusion regarding overring Equals for mutable objects that are used in data bound collections

Background: I've written a large scale WPF application using MVVM and it's been suffering from some intermittent problems. I initially asked the 'An item with the same key has already been added' Exception on selecting a ListBoxItem from code…
Sheridan
  • 68,826
  • 24
  • 143
  • 183
5
votes
3 answers

bitwise operator >>> in hashCode

I have two related questions: the bitwise operator >>> means that we are shifting the binary number by those many places while filling 0 in the Most Significant Bit. But, then why does the following operation yields the same number: 5>>>32 yields 5…
Gaurav
  • 1,570
  • 4
  • 20
  • 25
5
votes
2 answers

Is there a way to reduce amount of boilerplate code in Equals and GetHashCode?

I frequently have to override Equals and GetHashCode methods for the purpose of unit testing. After this my classes begin to look like this: public class TestItem { public bool BoolValue { get; set; } public DateTime DateTimeValue { get;…
Mike
  • 2,468
  • 3
  • 25
  • 36
4
votes
4 answers

Reverse Engineering String.GetHashCode

String.GetHashCode's behavior is depend on the program architecture. So it will return one value in x86 and one value on x64. I have a test application which must run in x86 and it must predict the hash code output from an application which must run…
Kenn
  • 2,379
  • 2
  • 29
  • 38
4
votes
4 answers

How can Contains returns false but GetHashCode() returns the same number, and Equals returns true?

I have an entity class like this (with lots of stuff missing): class Parent { private readonly Iesi.Collections.Generic.ISet children = new Iesi.Collections.Generic.HashedSet(); public virtual void AddChild(Child…
Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
4
votes
3 answers

Why we implement GetHashCode in IEqualityComparer?

I want to get distinct items from List in C# by using IEqualityComparer interface. But I don't know about GetHashCode. I have implement both GetHashCode and Equals methods. And how can I call Equals method to get distinct items from a list having…
NASSER
  • 5,900
  • 7
  • 38
  • 57
4
votes
3 answers

Should .GetHashCode() return same value for two objects having different refences in the memory?

I need to override Equals() method for one of my types but it seems I have to also override GetHashCode() method. I am not sure: If I have type Animal and if I have 2 instances of Animal which are basically the same(equal)Cats; like: Animal cat_01 =…
pencilCake
  • 51,323
  • 85
  • 226
  • 363