gethashcode

Overriding IEquatable<T> when T is an interface and hashcodes are different between derived types

不羁岁月 提交于 2021-02-19 07:34:08
问题 I have A and B classes both implementing interface I . public interface I { int SomeInt { get; } bool SomeBool { get; } float SomeFloat { get; } } public class A : I { public int SomeInt { get; } public bool SomeBool { get; } public float SomeFloat { get; } private readonly string _someARelatedStuff; // Rest of class... } public class B : I { public int SomeInt { get; } public bool SomeBool { get; } public float SomeFloat { get; } private string readonly _someBRelatedStuff; private double

Overriding IEquatable<T> when T is an interface and hashcodes are different between derived types

﹥>﹥吖頭↗ 提交于 2021-02-19 07:33:44
问题 I have A and B classes both implementing interface I . public interface I { int SomeInt { get; } bool SomeBool { get; } float SomeFloat { get; } } public class A : I { public int SomeInt { get; } public bool SomeBool { get; } public float SomeFloat { get; } private readonly string _someARelatedStuff; // Rest of class... } public class B : I { public int SomeInt { get; } public bool SomeBool { get; } public float SomeFloat { get; } private string readonly _someBRelatedStuff; private double

Generating a good hash code (GetHashCode) for a BitArray

非 Y 不嫁゛ 提交于 2021-02-19 02:02:32
问题 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, as in this scenario? UPDATE: The approach I originally took was to access the internal array of ints directly through reflection (speed is more important than encapsulation in this case), then XOR those values. The XOR approach seems to work well

GetHashCode() override coliding way to often

本小妞迷上赌 提交于 2021-02-08 04:09:00
问题 I'm using unity, and unity does not have a tuple in it, so I created my own tuple class to work since I needed it for my Dictionary. Dictionary <Tuple<int,int>, Tile> Tile class that I created and isn't really relevant to solve this problem(at least I think it wont help). But the problem is that I'm using both negative and positive integer in my tuples, and when I use my current GetHashCode() with the Tuples , sometimes I get the same HashCode, for example Tuple<-10, 8> and Tuple<-9,-10> both

Implementation of Object.GetHashCode()

喜你入骨 提交于 2021-02-07 11:23:06
问题 I'm reading Effective C# and there is a comment about Object.GetHashCode() that I didn't understand: Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object created is assigned a unique object key, stored as an integer, when it is created. These keys start at 1 and increment every time a new object of any type gets created. The object identity field is set in the System.Object constructor and cannot be modified later. Object.GetHashCode()

HashCode on decimal with IEqualityComparer in a ConcurrentDictionary

这一生的挚爱 提交于 2020-02-08 06:53:09
问题 I made a class to be used as a key in a dictionary. public class FourUintsOneDecimalKeyInfo { public uint IdOne { get; set; } public uint IdTwo { get; set; } public uint IdThree { get; set; } public uint IdFour { get; set; } public decimal DefinitionOne { get; set; } public class EqualityComparerAssetTableInfo : IEqualityComparer<FourUintsOneDecimalKeyInfo> { public bool Equals(FourUintsOneDecimalKeyInfo x, FourUintsOneDecimalKeyInfo y) { return x.IdOne == y.IdOne && x.IdTwo == y.IdTwo && x

LINQ - Distinct is ignored?

感情迁移 提交于 2020-01-15 04:35:07
问题 So I have a problem with my LINQ code, where I have to select a Distinct data set, I implement the following IEqualityComparer : public class ProjectRoleComparer : IEqualityComparer<ProjectUserRoleMap> { public bool Equals(ProjectUserRoleMap x, ProjectUserRoleMap y) { return x.RoleID.Equals(y.RoleID); } public int GetHashCode(ProjectUserRoleMap obj) { return obj.GetHashCode(); } } In this context, I wish to retrieve a bunch of ProjectUserRoleMap objects related to a given Project, identified

Override Equals and GetHashCode in class with one field

点点圈 提交于 2020-01-05 02:55:08
问题 I have a class: public abstract class AbstractDictionaryObject { public virtual int LangId { get; set; } public override bool Equals(object obj) { if (obj == null || obj.GetType() != GetType()) { return false; } AbstractDictionaryObject other = (AbstractDictionaryObject)obj; if (other.LangId != LangId) { return false; } return true; } public override int GetHashCode() { int hashCode = 0; hashCode = 19 * hashCode + LangId.GetHashCode(); return hashCode; } And I have derived classes: public

C# how to calculate hashcode from an object reference

半城伤御伤魂 提交于 2020-01-02 00:44:43
问题 Folks, here's a thorny problem for you! A part of the TickZoom system must collect instances of every type of object into a Dictionary<> type. It is imperative that their equality and hash code be based on the instance of the object which means reference equality instead of value equality. The challenge is that some of the objects in the system have overridden Equals() and GetHashCode() for use as value equality and their internal values will change over time. That means that their Equals and

Reverse Engineering String.GetHashCode

自古美人都是妖i 提交于 2019-12-30 06:55:16
问题 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 on x64. Below is the disassembly of the String.GetHashCode implementation from mscorwks. public override unsafe int GetHashCode() { fixed (char* text1 = ((char*) this)) { char* chPtr1 = text1; int num1 = 0x15051505; int num2 = num1; int* numPtr1 =