gethashcode

IEqualityComparer for Value Objects

I have an immutable Value Object, IPathwayModule, whose value is defined by: (int) Block; (Entity) Module, identified by (string) ModuleId; (enum) Status; and (entity) Class, identified by (string) ClassId - which may be null. Here's my current IEqualityComparer implementation which seems to work in a few unit tests. However, I d...

GetHashCode for a Class with a List Object

I have such a class: public class Cycle { public List<int> Edges { get; private set; } public override bool Equals(object obj) { Cycle cycle = (Cycle)obj; var list1 = cycle.Edges; var list2 = Edges; var same = list1.Except...

C#: How would you unit test GetHashCode?

Testing the Equals method is pretty much straight forward (as far as I know). But how on earth do you test the GetHashCode method? ...

C# - How to override GetHashCode with Lists in object

Hi, I am trying to create a "KeySet" to modify UIElement behaviour. The idea is to create a special function if, eg. the user clicks on an element while holding a. Or ctrl+a. My approach so far, first lets create a container for all possible modifiers. If I would simply allow a single key, it would be no problem. I could use a simple D...

Is there a built-in IEqualityComparer that compares objects only using their hash value?

Is there a built-in IEqualityComparer that compares objects by the value returned by their GetHashCode value? It's easy to write, but I'd prefer to use a provided class instead of a custom one. Current code: private class HashComparer : IEqualityComparer<TKey> { private readonly Func<TKey, int> _Hasher; public HashComparer (Func...

Is there a .Net 1.1 compatible String.GetHashCode implemented in .Net 2.0 code?

I have an existing app in which I made the mistake of using String.GetHashCode and persisting it to disk. Now that I'm upgrading the app to .Net 2.0 I find that that decision has come back to bite me in the butt. I'm interested to know if anyone knows about a .Net 2.0 implementation of a .Net 1.1 compatible string hashing algorithm. O...

Does DateTime.Now have its own implementation of GetHashCode that gives unique hashes?

The MSDN article here states, that the default implementation of GetHashCode() does not guarantee unique results and should be not used as an identifier. So my question is whether DateTime.Now has its own implementation that would give out unique hashes. Thx for help ...

Getting KeyNotFoundException when using key previously retrieved from key collection??

I've got the following code where for some reason I'm getting a KeyNotFoundException even though I'm using a key that I had retrived a couple of lines above. Does anyone know of a situation where this wouldn't work? I'm stumped. BTW 'SchemaElementType is an enum. public class DefaultValue { private Dictionary<Parameter, string> _para...

How to use Object.GetHashCode() on a type that overrides GetHashCode()

Hi, I have a class A that implements IEquatable<>, using its fields (say, A.b and A.c) for implementing/overriding Equals() and overriding GetHashCode(), and everything works fine, 99% of the time. Class A is part of a hierarchy (class B, C) that all inherit from interface D; they can all be stored together in a dictionary Dictionary, t...

Overloading GetHashCode and the equality operator using the XOR operator on enums

I have the following class which is part of a statictical analysis package. The MetricKey object is used as a dictionary key. Decision, MetricUnit & Portfolio are all enums. I had to override the equality operator (==) to get dictionary key matching working. I used the guidance at http://msdn.microsoft.com/en-us/library/ms173147.aspx...

What's the best strategy for Equals and GetHashCode?

I'm working with a domain model and was thinking about the various ways that we have to implement this two methods in .NET. What is your preferred strategy? This is my current implementation: public override bool Equals(object obj) { var newObj = obj as MyClass; if (null != newObj) { return ...

GetHashCode on null fields?

How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public Address As String Public Overloads Function Equals(ByVal other As Contact) As Boolean _ ...

Why should the "prime-based" hashcode implmentation be used instead of the "naive" one?

I have seen that a prime number implmentation of the GetHashCode function is being recommend, for example here. However using the following code (in VB, sorry), it seems as if that implementation gives the same hash density as a "naive" xor implementation. If the density is the same, I would suppose there is the same probability of cllis...

Is the .Net HashSet uniqueness calculation completely based on Hash Codes?

I was wondering whether the .Net HashSet<T> is based completely on hash codes or whether it uses equality as well? I have a particular class that I may potentially instantiate millions of instances of and there is a reasonable chance that some hash codes will collide at that point. I'm considering using HashSet's to store some instance...

How to implement IEquatable<T> when mutable fields are part of the equality - Problem with GetHashCode

Hello! I am using Entity Framework in my application. I implemented with the partial class of an entity the IEquatable<T> interface: Partial Class Address : Implements IEquatable(Of Address) 'Other part generated Public Overloads Function Equals(ByVal other As Address) As Boolean _ Implements System.IEquatable(Of Address).Equa...

Does c# have the same issues like Java with equals and gethashcode() ?

Does c# have the same issues like Java with equals and gethashcode? issues like: http://onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html ...

Dictionary keys don't contain a key that's already contained in keys

Why is the following 'exist' boolean variable getting a value of false??? foreach (Cell existCell in this.decoratorByCell.Keys) { //this call yield the same hashcode for both cells. still exist==false bool exist = this.decoratorByCell.ContainsKey(existCell); } I've overridden GetHashCode() & ...

NHibernate set : Should I override Equals and GetHashCode ?

I am new to NHibernate. I am using <set ... > mapping for some many-to-one and many-to-many associations. These are exposed as properties of type ICollection<T>, in practice implemented by HashSet<T>. My question is, should I override Equals and GetHashCode for the related types, so they match the database identity of the types (in pra...

What must be done to use the value of a reference type as a dictionary key?

Suppose I have a class T that I want to use as a key in a Dictionary<T,U> collection. What must I implement in T so that these keys are based on values of T rather than T references? I'm hoping it's just GetHashCode(). ...

Why does C# not implement GetHashCode for Collections?

I am porting something from Java to C#. In Java the hashcode of a ArrayList depends on the items in it. In C# I always get the same hashcode from a List... Why is this? For some of my objects the hashcode needs to be different because the objects in their list property make the objects non-equal. I would expect that a hashcode is alwa...