gethashcode

C# how to calculate hashcode from an object reference.

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 ...

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

Hi, 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 overwrite the Equals() and GetHashCode() Method but that doesn’t really work. Thought it’s something everybody does but I don’t find anything about the problem Online. When I write my own Classes and E...

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

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 ...

Is it totally fine to use a mutable object as a key in a Dictionary?

Say I have some special class, WrappedDataTable, and I want to associate each WrappedDataTable with exactly one DataTable. Furthermore, I want there to be no more than one WrappedDataTable in existence for any given DataTable. A colleague suggested I could cache my WrappedDataTable and use a factory method to access one, like this: pub...

Generating a good hash code (GetHashCode) for a BitArray

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...

How to override GetHashCode() for value types in a good way?

Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? As I understand it, there's a recommendation to override the base type GetHashCode() for my own value types. I have a hard time finding good resources on the actual how to implement a good hash code scheme for different kinds of value t...

GetHashCode with transient/persistent entities

Hiya, I'm implementing a layered supertype that all my entities will be based on. I am currently working on the Equity methods Equals and GetHashCode. I have the following for GetHashCode: public override int GetHashCode() { if (!_hashCode.HasValue) { if (this.Id.Equals(default(T))) { ...

Compiler Generated GetHashCode()

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 v...

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 somewhat performant hopefully. Thanks ...

Equals, GetHashCode, EqualityComparers and fuzzy equality

For an object with properties A, B, C, D, StartDate and EndDate if I wanted to implement something where any two objects are equal if they have identical A, B and C and overlapping date range, how would that be done? I have tried creating an EqualityComparer like so public override bool Equals(RateItem x, RateItem y) { ...

GetHashCode() with string keys

Hey all, I've been reading up on the best way to implement the GetHashCode() override for objects in .NET, and most answers I run across involve somehow munging numbers together from members that are numeric types to come up with a method. Problem is, I have an object that uses an alphanumeric string as its key, and I'm wondering if the...

Why returning false ? new Person("james") == new Person("james") ?

I have override GetHashCode and Equals and both methods provide same results for different objects but why still getting false ? class Program { static void Main(string[] args) { Console.WriteLine(new Person("james") == new Person("james")); Console.ReadKey(); } } class Person { private strin...

What would be a good hashCode for a DateRange class

I have the following class public class DateRange { private DateTime startDate; private DateTime endDate; public override bool Equals(object obj) { DateRange other = (DateRange)obj; if (startDate != other.startDate) return false; if (endDate != other.endDate) return false; ...

Gethashcode() function

Why aren't C1 and c2 have the same hashcode ? the code doesn't get to "Same".... ( i=0 in both classes) class myclass { public static int i; static void Main() { myclass c1 = new myclass(); myclass c2 = new myclass(); if (c1.GetHashCode() == c2.GetHashCode()) Console.Write("Same"); ...

How to calculate the hash code of a string by hand (Java related)

Hi guys, I was wondering how to calculate the hash code for a given string by hand. I understand that in Java, you can do something like: String me = "What you say what you say what?"; long whatever = me.hashCode(); That's all good and dandy, but I was wondering how to do it by hand. I know the given formula for calculating the hash ...

Why is ValueType.GetHashCode() implemented like it is?

From ValueType.cs **Action: Our algorithm for returning the hashcode is a little bit complex. We look ** for the first non-static field and get it's hashcode. If the type has no ** non-static fields, we return the hashcode of the type. We can't take the ** hashcode of a static member because if that member is of...

Ways to keep abstract method server side for WCF

We are enforcing all our domain objects to implement GetHashCode. namespace Core { [Serializable] public abstract class DomainObject { public abstract override int GetHashCode(); } } namespace Entity.Domain { [Serializable] [DataContract] public partial class IdCard : DomainObject { private System.Int32 _effDte;...

wpf overriding getHashCode and Eqaul in ContentControl

Hi I have a class which derives from ContentControl and I'm not able to override GetHashCode and Equal method. I get an error Error 5 cannot override inherited member 'System.Windows.DependencyObject.GetHashCode()' because it is sealed Is there any way to override this method ? I need to use Union method from LINQ however I need to c...

GetHashCode Equality

I've wondered about this, so I figure I'll ask it. Most places you'll see use the same semantic logic for overriding Equals as GetHashCode for memberwise equality...however they usually use different implementations: public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { ...

Double.GetHashCode algorithm or override

hello i have an application project that both managed and unmanaged code runs and i need to use the same algorithm for hashing double values in both systems. so either i will override System.Double.GetHashCode() or use its algorithm in c++ code. i could not find the double.gethashcode algorithm and decided to override the function. but ...