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
1
vote
1 answer

Why does foreach calls GetHashCode?

I was surprised when I found that my override GetHashCode() method of class Foo is called when I am using foreach to travel in IEnumerable. But it doesn't happens in others cases. Why? Some parts of real code: var…
Brans Ds
  • 4,039
  • 35
  • 64
1
vote
7 answers

IEqualityComparer for Value Objects

I have an immutable Value Object, IPathwayModule, whose value is defined by: (int) Block; (Entity) Module, identified by (string) ModuleId; (enum) Status; and (entity) Class, identified by (string) ClassId - which may be null. Here's my…
djskinner
  • 8,035
  • 4
  • 49
  • 72
1
vote
3 answers

Using GetHashCode() for a random, persistent index

I have a string which identifies a customer. On my website, I have 3 different images for customers who have not set a profile picture. I want to randomly associate one of these images to a customer, but not have the image change between…
Spongeboy
  • 2,232
  • 3
  • 28
  • 37
1
vote
1 answer

ActiveRecord and GetHashCode fails if int is nullable

There is a problem when the int is nullable on GetHashCode At the point of GetHashCode on ActiveRecord.tt there is a need for a nullable check. Something like this. <# if(tbl.PK.SysType=="int" && !tbl.PK.Nullable ){ #> …
Aristos
  • 66,005
  • 16
  • 114
  • 150
1
vote
3 answers

Correct way to override Equals(object obj) when dealing with sub/superclasses?

I am using the following code to test for Equals public override bool Equals(object obj) { // Equals must return false on compares to null. if (obj == null || GetType() != obj.GetType()) return false; Foo fooItem = obj as…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
1
vote
1 answer

Overriding Equals and GetHashcode and double comparison

I have a problem with equality and adding objects to dictionary class DoublePoint { public double X; public double Y; public double Z; public DoublePoint(double x, double y, double z) { this.X = x; this.Y = y; this.Z =…
Harsha
  • 1,861
  • 7
  • 28
  • 56
1
vote
4 answers

How to implement a GetHashCode compatible Equals method, when the space is greater than 32 bits?

In .NET you need that Equals(object) and GetHashCode() are compatible. But sometimes you can't: public class GreaterThan32Bits { public int X { get; set; } public int Y { get; set; } } Because the data density is greater than 32 bits, and…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
1
vote
2 answers

Can a custom GetHashcode implementation cause problems with Dictionary or Hashtable's "buckets"

I'm considering implementing my own custom hashcode for a given object... and use this as a key for my dictionary. Since it's possible (likely) that 2 objects will have the same hashcode, what additional operators should I override, and what…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
1
vote
2 answers

C# GetHashCode with two Int16, also returns only up to Int32?

Sorry to combine two questions into one, they are related. HashCodes for HashSets and the such. As I understand it, they must be unique, not change, and represent any configuration of an object as a single number. My first question is that for my…
alan2here
  • 3,223
  • 6
  • 37
  • 62
1
vote
1 answer

Overload Equals / GetHashCode in VB.NET to Use Objects as Dictionary Keys

I have a Dictionary whose keys are Excel Range objects (no, this is not negotiable), defined as follows (the type CellProp is an object that contains various cell properties): Dim dic As New Dictionary(Of Excel.Range, CellProp)(New…
OfficeAddinDev
  • 1,105
  • 3
  • 15
  • 29
0
votes
4 answers

Why the GetHashCode does not take advantage of SK.exe tool's hashcode algorithm?

MSDN says: "The default implementation of the GetHashCode method does not guarantee unique return values for different objects. " But on the other hand, when I use the sn.exe tool it ensures a unique hash value to create a strongly-named assembly.…
pencilCake
  • 51,323
  • 85
  • 226
  • 363
0
votes
1 answer

Implementing GetHashCode on a value class

I have a class Money and I want to know what the best way of implementing GetHashCode on this value class would be give that $1 != €1. Having a weighted value against the currency * value is not going to work. public class Money :…
user1054637
  • 695
  • 11
  • 28
0
votes
2 answers

GetHashCode Method reliability in Silverlight/WP7.1

I am attempting to hash and keep(the hash) an object of type IEnumerable which has about a 1000 entries. I'll be generating another such object, but this time I'd like to check for any changes in the values of the entries using the…
abhinav
  • 3,199
  • 2
  • 21
  • 25
0
votes
4 answers

Is .GetHashCode guaranteed to be the same across systems/platform versions?

Possible Duplicate: Can I depend on the values of GetHashCode() to be consistent? If I use the Object.GetHashCode() method across two systems/framework versions, am I guaranteed to get the same value for the same input? In other words, does its…
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
0
votes
1 answer

Distinct is not working with IEquatable

I have a class Bar that looks like this: public class Bar : IEquatable { public string Stringbar1{ get; set; } public string Stringbar2{ get; set; } public string Stringbar3{ get; set; } public string Stringbar4{ get; set; } …
Mohamad Hammash
  • 237
  • 1
  • 8