Showing posts with label hashcode. Show all posts
Showing posts with label hashcode. Show all posts

Wednesday, May 30, 2012

Monday, May 21, 2012

Why is it important to override GetHashCode when Equals method is overriden in C#?


Given the following class




public class Foo
{
public int FooId { get; set; }
public string FooName { get; set; }

public override bool Equals(object obj)
{
Foo fooItem = obj as Foo;

return fooItem.FooId == this.FooId;
}

public override int GetHashCode()
{
// Which is preferred?

return base.GetHashCode();

//return this.FooId.GetHashCode();
}
}

Wednesday, May 9, 2012

What is a sensible prime for hashcode calculation?


Eclipse 3.5 has a very nice feature to generate Java hashCode() functions. It would generate for example (slightly shortened:)

Sunday, March 11, 2012

apache commons equals/hashcode builder


I'm curious to know, what people here think about using org.apache.commons.lang.builder EqualsBuilder/HashCodeBuilder for implementing the equals/hashcode? Would it be a better practice than writing your own? Does it play well with Hibernate? What's your opinion?