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

How to properly override equality logic and preserve data binding capabilities

I would like to have a reference on how to correctly implement a class that satisfies following requisites: implementing a logic for equality that depends from its content, i.e. its fields proven for data binding To satisfy point 1, I am used to…
Mauro Ganswer
  • 1,379
  • 1
  • 19
  • 33
3
votes
1 answer

How is GetHashCode Implemented for Booleans?

I was wondering how exactly they generate a hashcode from boolean types in C#/.NET?
Krythic
  • 4,184
  • 5
  • 26
  • 67
3
votes
1 answer

Does hash code is being stored in SyncBlockIndex/SyncBlock

Every object in .net has headers (SyncBlockIndex and MethodTablePointer), and when you call GetHashCode() the result could be saved in SyncBlockIndex if there is no linked SyncBlock to this object, or in SyncBlock if so. When we don't override…
Vasyl Senko
  • 1,779
  • 20
  • 33
3
votes
3 answers

Is there a guranteed uniqueness for GetType().GetHashCode()?

Let's say I have a few classes: MyClass1() {} MyChild1() : MyClass1 {} MyChild2() : MyClass2 {} MyGrandchild1() : MyChild2 {} etc. I know that GetHashCode() by itself, does not guarantee uniqueness between any two different Objects, but I'm…
avance70
  • 787
  • 1
  • 11
  • 22
3
votes
3 answers

Equals, GetHashCode, EqualityComparers and fuzzy equality

For an object with properties A, B, C, D, StartDate and EndDate if I wanted to implement something where any two objects are equal if they have identical A, B and C and overlapping date range, how would that be done? I have tried creating an…
Tim Gray
  • 33
  • 2
3
votes
3 answers

In Java object's hashcode is how to generate?

In Java, is an Object's hashcode value generated by algorithm according to the content of the object or according to the object instance memory address?
wanghao
  • 271
  • 1
  • 7
  • 21
3
votes
1 answer

Compiler Generated GetHashCode()

I'm working on writing a compiler for a language running on .net and one of the things I would like it to do is to automatically generate the GetHashCode method, but I have several questions: Is this possible, does the compiler know enough about…
Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
3
votes
4 answers

What must be done to use the value of a reference type as a dictionary key?

Suppose I have a class T that I want to use as a key in a Dictionary collection. What must I implement in T so that these keys are based on values of T rather than T references? I'm hoping it's just GetHashCode().
Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
3
votes
1 answer

How do I Override GetHashCode and CompareTo for a Discriminated Union in F#?

I have a simple F# discriminated union that combines a bool, a string, and a float. I want to override the Object.Equals(arg) of this union so that I can put in an epsilon to account for precision errors when checking for float equality. The…
user2023861
  • 8,030
  • 9
  • 57
  • 86
3
votes
3 answers

Overriding GetHashCode() for value objects without fields

Sometimes I need value objects without fields (message headers, schemas, etc.), for example: abstract class RequestHeader { } sealed class FirstRequestHeader : RequestHeader { } I use them in methods like: partial class Server { private…
astef
  • 8,575
  • 4
  • 56
  • 95
3
votes
1 answer

Implementing correct GetHashCode

I have the following class public class ResourceInfo { public string Id { get; set; } public string Url { get; set; } } which contains information about some resource. Now I need the possibility to check if two such resources are equal by…
steavy
  • 1,483
  • 6
  • 19
  • 42
3
votes
1 answer

HashSet limit - how to proceed?

My program creates custom objects, I want to get a distinct list of. So I want to use a set and add object by object. The set would prevent duplicates. And at last I have a set of unique objects. I would usually use a HashSet, because I don't need a…
Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73
3
votes
1 answer

Is there a .Net 1.1 compatible String.GetHashCode implemented in .Net 2.0 code?

I have an existing app in which I made the mistake of using String.GetHashCode and persisting it to disk. Now that I'm upgrading the app to .Net 2.0 I find that that decision has come back to bite me in the butt. I'm interested to know if anyone…
Steve Hiner
  • 2,523
  • 3
  • 24
  • 33
3
votes
4 answers

How to generate a unique hash for a collection of objects independent of their order

Let's say I have a class public class MyClass { public string Type { get; set; } public int Id { get; set; } } and I have a collection class that is simply a strongly typed List public class MyClassList : List { public…
Vadim
  • 17,897
  • 4
  • 38
  • 62
3
votes
1 answer

What can cause dictionary.ContainsKey(dictionary.Keys.First()) to return false?

dictionary.Keys.First().GetHashCode() == dictionary.Keys.First().GetHashCode() returns true dictionary.Keys.First() == dictionary.Keys.First() returns true What's missing? Why can't the dictionary find this object? Type of dictionary:…
MushinNoShin
  • 4,695
  • 2
  • 32
  • 46