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 =

Why might a System.String object not cache its hash code?

和自甴很熟 提交于 2019-12-29 04:27:12
问题 A glance at the source code for string.GetHashCode using Reflector reveals the following (for mscorlib.dll version 4.0): public override unsafe int GetHashCode() { fixed (char* str = ((char*) this)) { char* chPtr = str; int num = 0x15051505; int num2 = num; int* numPtr = (int*) chPtr; for (int i = this.Length; i > 0; i -= 4) { num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0]; if (i <= 2) { break; } num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1]; numPtr += 2; } return (num +

Is it possible to combine hash codes for private members to generate a new hash code?

◇◆丶佛笑我妖孽 提交于 2019-12-28 05:34:26
问题 I have an object for which I want to generate a unique hash (override GetHashCode()) but I want to avoid overflows or something unpredictable. The code should be the result of combining the hash codes of a small collection of strings. The hash codes will be part of generating a cache key, so ideally they should be unique however the number of possible values that are being hashed is small so I THINK probability is in my favour here. Would something like this be sufficient AND is there a

Null vs Empty Collections in GetHashCode

时光怂恿深爱的人放手 提交于 2019-12-24 19:33:37
问题 In the implementation of GetHashCode below, when Collection is null or empty will both result in a hash code of 0 . A colleague suggested return a random hard coded number like 19 to differentiate from a null collection. Why would I want to do this? Why would I care that a null or empty collection produces a different hash code? public class Foo { public List<int> Collection { get; set; } // Other properties omitted. public int override GetHashCode() { var hashCode = 0; if (this.Collection !=

Why can't I override GetHashCode on a many-to-many entity in EF4?

十年热恋 提交于 2019-12-23 21:32:18
问题 I have a many-to-many relationship in my Entity Framework 4 model (which works with a MS SQL Server Express): Patient-PatientDevice-Device. I'm using Poco, so my PatientDevice-class looks like this: public class PatientDevice { protected virtual Int32 Id { get; set; } protected virtual Int32 PatientId { get; set; } public virtual Int32 PhysicalDeviceId { get; set; } public virtual Patient Patient { get; set; } public virtual Device Device { get; set; } //public override int GetHashCode() //{

Dictionary using is custom key but key is always unequal

孤人 提交于 2019-12-23 03:34:06
问题 I am using RTBTextPointer as custom key in dictionary... Init.SpintaxEditorPropertyMain.SpintaxListDict = new Dictionary<RTBTextPointer, SpintaxEditorProperties.SpintaxMappedValue>(new RTBTextPointerComparer()); I worte this RTBTextPointer, and RTBTextPointerComparer classes in class library and using this in different wpf projects, if (Init.SpintaxEditorPropertyMain.SpintaxListDict.ContainsKey(_index) == false) { Init.SpintaxEditorPropertyMain.SpintaxListDict.Add(_index,_SpintaxMappedVal); }