gethashcode

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); }

Compiler Generated GetHashCode()

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 10:55:13
问题 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 the type(s) involved to do a reasonable job implementing the method? Should I do this for value types, reference types, or both? What is a reasonable GetHashCode algorithm for the compiler to generate which includes support for null properties and so

Why are these hashcodes equals?

余生长醉 提交于 2019-12-22 10:53:22
问题 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 hashCode2 = new { CustomerId = 1210, ServiceId = 3, CmsThematicId = (int?)591, StartDate = (DateTime?)new DateTime(2013, 3, 31), EndDate = (DateTime?)new DateTime(2013, 4, 1) }.GetHashCode(); Assert.AreNotEqual(hashCode, hashCode2); Can you tell me why ? 回答1: It's kinda

Have I implemented Equals()/GetHashCode() correctly?

北城余情 提交于 2019-12-21 09:29:22
问题 The program was working with this implementation: class Instrument { public string ClassCode { get; set; } public string Ticker { get; set; } public override string ToString() { return " ClassCode: " + ClassCode + " Ticker: " + Ticker + '.'; } } But because I need to use Instrument in Dictionary I've decided to implement equals/hashcode: class Instrument { public string ClassCode { get; set; } public string Ticker { get; set; } public override string ToString() { return " ClassCode: " +

Using GetHashCode to test equality in Equals override

☆樱花仙子☆ 提交于 2019-12-21 09:21:56
问题 Is it ok to call GetHashCode as a method to test equality from inside the Equals override? For example, is this code acceptable? public class Class1 { public string A { get; set; } public string B { get; set; } public override bool Equals(object obj) { Class1 other = obj as Class1; return other != null && other.GetHashCode() == this.GetHashCode(); } public override int GetHashCode() { int result = 0; result = (result ^ 397) ^ (A == null ? 0 : A.GetHashCode()); result = (result ^ 397) ^ (B ==

GetHashCode() problem using xor

≡放荡痞女 提交于 2019-12-21 04:11:24
问题 My understanding is that you're typically supposed to use xor with GetHashCode() to produce an int to identify your data by its value (as opposed to by its reference). Here's a simple example: class Foo { int m_a; int m_b; public int A { get { return m_a; } set { m_a = value; } } public int B { get { return m_b; } set { m_b = value; } } public Foo(int a, int b) { m_a = a; m_b = b; } public override int GetHashCode() { return A ^ B; } public override bool Equals(object obj) { return this