Quantcast
Channel: Why is it important to override GetHashCode when Equals method is overridden? - Stack Overflow
Browsing all 17 articles
Browse latest View live

Answer by John XU for Why is it important to override GetHashCode when Equals...

In .NET, when you override the Equals() method, it's recommended to also override GetHashCode(). The reason is related to how .NET uses GetHashCode in its built-in data structures.When you store an...

View Article


Answer by Hossein Ebrahimi for Why is it important to override GetHashCode...

As of C# 9(.net 5 or .net core 3.1), you may want to use records as it does Value Based Equality by default.

View Article


Answer by l33t for Why is it important to override GetHashCode when Equals...

As of .NET 4.7 the preferred method of overriding GetHashCode() is shown below. If targeting older .NET versions, include the System.ValueTuple nuget package.// C# 7.0+public override int GetHashCode()...

View Article

Answer by Vasil Kosturski for Why is it important to override GetHashCode...

You should always guarantee that if two objects are equal, as defined by Equals(), they should return the same hash code. As some of the other comments state, in theory this is not mandatory if the...

View Article

Answer by BornToCode for Why is it important to override GetHashCode when...

Just to add on above answers:If you don't override Equals then the default behavior is that references of the objects are compared. The same applies to hashcode - the default implmentation is typically...

View Article


Answer by Guanxi for Why is it important to override GetHashCode when Equals...

Below using reflection seems to me a better option considering public properties as with this you don't have have to worry about addition / removal of properties (although not so common scenario). This...

View Article

Answer by Ian Ringrose for Why is it important to override GetHashCode when...

We have two problems to cope with.You cannot provide a sensible GetHashCode() if any field in theobject can be changed. Also often a object will NEVER be used in acollection that depends on...

View Article

Answer by user2855602 for Why is it important to override GetHashCode when...

It's my understanding that the original GetHashCode() returns the memory address of the object, so it's essential to override it if you wish to compare two different objects. EDITED:That was incorrect,...

View Article


Answer by Maciej for Why is it important to override GetHashCode when Equals...

Hash code is used for hash-based collections like Dictionary, Hashtable, HashSet etc. The purpose of this code is to very quickly pre-sort specific object by putting it into specific group (bucket)....

View Article


Answer by huha for Why is it important to override GetHashCode when Equals...

Please don´t forget to check the obj parameter against null when overriding Equals().And also compare the type.public override bool Equals(object obj){ Foo fooItem = obj as Foo; if (fooItem == null) {...

View Article

Answer by ILoveFortran for Why is it important to override GetHashCode when...

It's not necessarily important; it depends on the size of your collections and your performance requirements and whether your class will be used in a library where you may not know the performance...

View Article

Answer by Ludmil Tinkov for Why is it important to override GetHashCode when...

How about:public override int GetHashCode(){ return string.Format("{0}_{1}_{2}", prop1, prop2, prop3).GetHashCode();}Assuming performance is not an issue :)

View Article

Answer by Albic for Why is it important to override GetHashCode when Equals...

It's actually very hard to implement GetHashCode() correctly because, in addition to the rules Marc already mentioned, the hash code should not change during the lifetime of an object. Therefore the...

View Article


Answer by Trap for Why is it important to override GetHashCode when Equals...

By overriding Equals you're basically stating that you know better how to compare two instances of a given type.Below you can see an example of how ReSharper writes a GetHashCode() function for you....

View Article

Answer by kemiller2002 for Why is it important to override GetHashCode when...

It is because the framework requires that two objects that are the same must have the same hashcode. If you override the equals method to do a special comparison of two objects and the two objects are...

View Article


Answer by Marc Gravell for Why is it important to override GetHashCode when...

Yes, it is important if your item will be used as a key in a dictionary, or HashSet<T>, etc - since this is used (in the absence of a custom IEqualityComparer<T>) to group items into...

View Article

Why is it important to override GetHashCode when Equals method is overridden?

Given the following classpublic class Foo{ public int FooId { get; set; } public string FooName { get; set; } public override bool Equals(object obj) { Foo fooItem = obj as Foo; if (fooItem == null) {...

View Article

Browsing all 17 articles
Browse latest View live


Latest Images