I want to generate the hash code for a object and store the generated hash code into the database. After some time I want to update the the object.Before updating the object, I want to check whether the object is changed or not using hash code. if the Generated has code and stored has codes are same I want to skip the updation, else I will update the Object. How can I implement this?
Asked
Active
Viewed 815 times
-2
-
1See: [What is the best algorithm for an overridden System.Object.GetHashCode?](http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode) – Habib May 07 '15 at 19:04
-
Note that it's not a fool-proof solution. A hash code can not _definitively_ tell you if an object hasn't changed since it can only hold 2^32 values, while an object with more than 4 bytes of data can have more possible states. You still need to check all field values to ensure that an object hasn't changed. – D Stanley May 07 '15 at 19:07
-
1See the identified duplicate, but also be careful with conflating a hash code with uniqueness, the two ideas are not the same. – Anthony Pegram May 07 '15 at 19:07
-
1@AnthonyPegram I am not sure if the duplicate is the best for the OP. He is persisting the hash value to a database, you should never persist values from built in .NET classes' `.GetHashCode()`. The resultant value is not guaranteed to be the same across AppDomains (which it is how a result from `GetHashCode()` should behave). The duplicate you linked to relies heavily on calling `.GetHashCode()` on classes that are built in to the .net framework. – Scott Chamberlain May 07 '15 at 20:54
-
@PavankumarKulkarni I will not re-open your question in its current state, while I do think it is not a duplicate of the linked question I also think it too broad and should be closed for that reason. If you re-write your question to narrow its focus and explain the specific problem you are having implementing this yourself I will re-open your question. – Scott Chamberlain May 07 '15 at 20:59
-
@ScottChamberlain In addition, a hash code is not a good mechanism to detect _changes_. It's a good quick way to determine if two objects _might_ be equal, but if you need to detect _changes_ you're going to have to check each attribute value. – D Stanley May 07 '15 at 21:15
1 Answers
0
You can use xor to include all the relevant properties' hash codes.
void Main() {
var emp = new Employee {
Id = 123,
FirstName = "Billy",
LastName = "Bobby", // lol, it's actually two first names
};
int originalHash = emp.GetHashCode();
emp.FirstName = "Timmy";
Console.WriteLine ("Original: {0}, Current: {1}", originalHash, emp.GetHashCode());
}
class Employee {
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public override int GetHashCode() {
return this.Id.GetHashCode()
^ this.FirstName.GetHashCode()
^ this.LastName.GetHashCode();
}
public override bool Equals(object other) {
var otherEmployee = other as Employee;
return otherEmployee != null
&& otherEmployee.Id == this.Id
&& otherEmployee.FirstName == this.FirstName
&& otherEmployee.LastName == this.FirstName;
}
}

recursive
- 83,943
- 34
- 151
- 241
-
He is persisting the hash code to a database. `String.GetHashCode()` can return different values between runs of the program for the same string, it is only gaurnteed to be the same result within multiple calls within a single AppDomain. – Scott Chamberlain May 07 '15 at 20:55
-
This is a very bad implementation if the goal is to detect changes. If you switch the first name and last name, the hash code will be exactly the same, and will look like the object did not change. – D Stanley May 07 '15 at 21:13
-
@DStanley: All implementations are bad implementations if the goal is to detect changes. There are only 2^32 possible hash codes. – recursive May 07 '15 at 21:16
-
@recursive true, but this one is worse since there's a _much_ higher probability of collision. – D Stanley May 07 '15 at 21:33