473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how do i correctly impl GetHashCode for syntactically equal object

I have some business objects which overrides Equals to provide syntax
equality instead of just reference equality. Overriding equals gives me a
warning that i must override GetHashcode(). Msdn says this about implementing
hashcode:

(1)If two objects of the same type represent the same value, the hash
function must return the same constant value for either object.
(2)For the best performance, a hash function must generate a random
distribution for all input.
(3)The hash function must return exactly the same value regardless of any
changes that are made to the object.

Since object.GetHashCode() is implemented using the object SYNCBLOC (i
think), it fall short when syntax equality is introduced (see req nr 1). The
problem is that i can't really se how i can combine req 1 and 3 if the object
is not immutable. Req 3 (as i interpret it) basically states that the
hashcode must be calculated based on member variables when the object is
initialized and it should not reflect changes to the object data. Now
consider the following: two objects are initialised with different values and
their hashes are calculated. We change the values of one of the objects so
that both objects are syntactically equal. Since the hashcodes we calculated
were based on the initialized values, req 1 is breached and the new
implementation is as bad as the original. Is there a way to implement
GetHashcode() correctly for mutable syntactically equal reference types?
Perhaps i am misinterpreting the implementation guidelines?
Jan 20 '06 #1
1 2172
Hello MariusI,
I have some business objects which overrides Equals to provide syntax
equality instead of just reference equality. Overriding equals gives
me a warning that i must override GetHashcode(). Msdn says this about
implementing hashcode:

(1)If two objects of the same type represent the same value, the hash
function must return the same constant value for either object. (2)For
the best performance, a hash function must generate a random
distribution for all input. (3)The hash function must return exactly
the same value regardless of any changes that are made to the object.

Since object.GetHashCode() is implemented using the object SYNCBLOC (i
think), it fall short when syntax equality is introduced (see req nr
1). The problem is that i can't really se how i can combine req 1 and
3 if the object is not immutable. Req 3 (as i interpret it) basically
states that the hashcode must be calculated based on member variables
when the object is initialized and it should not reflect changes to
the object data. Now consider the following: two objects are
initialised with different values and their hashes are calculated. We
change the values of one of the objects so that both objects are
syntactically equal. Since the hashcodes we calculated were based on
the initialized values, req 1 is breached and the new implementation
is as bad as the original. Is there a way to implement GetHashcode()
correctly for mutable syntactically equal reference types? Perhaps i
am misinterpreting the implementation guidelines?


Equality is a *semantical* concept.

What these rules basically imply that a type must have a "basic" identity
which cannot be changed after its construction in order to be support Equals()
and GetHashCode(). Think of a Person object that lives in a RDBMS like SQL
Server:

public class Person {
private int primaryKey;
private string firstName;
private string lastName;
private DateTime age;

// And similar named public Properties PrimaryKey, FirstName, LastName, Age
}

The following version of Person does not allow for such a basic identity:

public class Person {
// private fields as above

public Person() {}

public PrimaryKey { get; set; }
}

This basically wraps arbitrary records from the database, and can switch
identities each time you assign it a new PrimaryKey. This kind of class cannot
support both rules (1) and (3).

So what you need is to make such classes partially immutable:

public class Person {
// private fields as above, but
private readonly int primaryKey;

public Person(PrimaryKey primaryKey) {
this.primaryKey = primaryKey;
}

public PrimaryKey { get; }

// Equals() and GetHashCode()
}

This also implies that hash code calculation only uses immutable members
of the object.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Jan 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Guinness Mann | last post by:
resource.cs(8,15): warning CS0659: 'UA.LMS.Resource.Resource' overrides Object.Equals(object o) but does not override Object.GetHashCode() What does this mean? I mean I know what it says, but do...
7
by: Avin Patel | last post by:
Hi I have question for GetHashCode() function, Is it correct in following code or there is more efficient way to implement GetHashCode() function class IntArray public int data public...
3
by: cmrchs | last post by:
Hi, why is it requested that when Equals() is implemented in a class that GethashCode() must be implemented as well ? thnx Chris ...
12
by: Elhanan | last post by:
hi.. i wanted to build a Dictionary Classs that will load my own class called letter, i understood that i implement the IEquatable interface's equles method that then the dictionary would use...
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
1
by: eramfelt | last post by:
Why doesnt ArrayList implements the Equals() or GetHashCode() methods? How is the developer supposed to check if an array list is equal to another array list? Is the developer supposed to iterate...
28
by: Tony Johansson | last post by:
Hello! I can't figure out what point it is to use GetHashCode. I know that this GetHashCode is used for obtaining a unique integer value. Can somebody give me an example that prove the...
10
by: Tony | last post by:
Hello! Here I have a Card class with several methods. Now to my question I don't really fully understand what purpose this GetHashCode is used for. That's why I bring it up again. In this...
6
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a moderately complex class (about 10 different string and int type fields along with two different List<stringfields). I want to override Equals(), so I need to override GetHashCode()....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.