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
4
votes
2 answers

Unit Testing - What do you do when your code is pretty much just a calculation (GetHashCode for example)?

public class Foo { public int X { get; set; } public int Y { get; set; } public int Z { get; set; } public override int GetHashCode() { var hash = 17; hash *= 23 + x.GetHashCode(); hash *= 23 +…
myermian
  • 31,823
  • 24
  • 123
  • 215
4
votes
2 answers

Why does VS 2017 generate GetHashCode without an unchecked block

I recently discovered that Visual Studio 2017 can auto-generate overrides for Equals and GetHashCode, but I was wondering why the GetHashCode implementation is not in an unchecked block? I made a simple class with two public string properties Foo…
TJ Rockefeller
  • 3,178
  • 17
  • 43
4
votes
0 answers

TypeScript Set with custom equals method

I'm currently trying to do the following. I am using a Set to store objects. Now as Typescript uses SameZeroEquality for comparing Objects the default has will not work. So I am trying to build an AdvancedSet that defines a custom has method. So far…
relief.melone
  • 3,042
  • 1
  • 28
  • 57
4
votes
2 answers

Using F#'s hash function inside GetHashCode() evil?

I encountered a couple of places online where code looked something like this: [] type Test = | Foo | Bar override x.Equals y = match y with | :? Test as y' -> match y' with …
Abel
  • 56,041
  • 24
  • 146
  • 247
4
votes
1 answer

Good hash for small class? (override GetHashCode)

I use some identity classes/structs that contains 1-2 ints, maybe a datetime or a small string as well. I use these as keys in a dictionary. What would be a good override of GetHashCode for something like this? Something quite simple but still…
KOT
  • 1,986
  • 3
  • 21
  • 35
4
votes
1 answer

Entity Framework 4 overwrite Equals and GetHashCode of an own class property

I’m using Visual Studio 2010 with .NET 4 and Entity Framework 4. I’m working with POCO Classes and not the EF4 Generator. I need to override the Equals() and GetHashCode() methods, but that doesn’t really work. I thought it’s something everybody…
Zhok
  • 65
  • 5
4
votes
1 answer

Handling collections in GetHashCode implementation

I'm working on implementing GetHashCode() based on the HashCode struct in this answer here. Since my Equals method will consider collections using Enumerable.SequenceEqual(), I need to include the collections in my GetHashCode() implementation. As a…
brdmllr
  • 43
  • 1
  • 6
4
votes
2 answers

NHibernate set : Should I override Equals and GetHashCode?

I am new to NHibernate. I am using mapping for some many-to-one and many-to-many associations. These are exposed as properties of type ICollection, in practice implemented by HashSet. My question is, should I override Equals and…
driis
  • 161,458
  • 45
  • 265
  • 341
4
votes
1 answer

What is causing this implementation of GetHashCode to be 20 times slower than .net's implementation?

I got the idea of a Substring struct from this post and this one. The second post has the implementation of .net's String.GetHashCode(). (I'm not sure which version of .net this is from.) Here is the implementation. (GetHashCode is taken from the…
bright
  • 4,700
  • 1
  • 34
  • 59
4
votes
3 answers

Does DateTime.Now have its own implementation of GetHashCode that gives unique hashes?

The MSDN article here states, that the default implementation of GetHashCode() does not guarantee unique results and should be not used as an identifier. So my question is whether DateTime.Now has its own implementation that would give out unique…
Jaker
  • 43
  • 1
  • 3
4
votes
1 answer

Should GetHashCode() method take care about null value given as parameter?

In some C# code, I use linq GroupBy() method with a custom IEqualityComparer. GroupBy(x => x.SomeField, new FooComparer()); The field i use as a grouping key can be null. As a consequence, i had to add some null checks in Equals()…
tigrou
  • 4,236
  • 5
  • 33
  • 59
4
votes
5 answers

Why are hashcodes different when two objects of the same type have the same values?

It is my understanding that GetHashCode will return the same value for two different instances which share the same values. The MSDN documentation is a bit fuzzy on this point. A hash code is a numeric value that is used to identify an object …
Chuck Conway
  • 16,287
  • 11
  • 58
  • 101
4
votes
4 answers

Why are these hashcodes equals?

This test is failing : var hashCode = new { CustomerId = 3354, ServiceId = 3, CmsThematicId = (int?)605, StartDate = (DateTime?)new DateTime(2013, 1, 5), EndDate = (DateTime?)new DateTime(2013, 1, 6) }.GetHashCode(); var…
remi bourgarel
  • 9,231
  • 4
  • 40
  • 73
4
votes
4 answers

Can I trace object identity using GetHashCode?

What is the use of GetHashCode()? Can I trace object identity using GetHashCode()? If so, could you provide an example?
user160677
  • 4,233
  • 12
  • 42
  • 55
4
votes
2 answers

What are the drawbacks of using a Guid().GetHashCode() when overriding GetHashCode()

I found an implementation of GetHashCode() that looks like this Guid _hashCode = Guid.NewGuid(); public override int GetHashCode() { return _hashCode.GetHashCode(); } Even thought the Equals looks correct, is it correct to…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448