<p>I have an object that is mapped to a cookie as a serialized base-64 string. I only want to write out a new cookie if there are changes made to the object stored in the cookie on server-side.</p>

<p>What I want to do is get a hash code when the object is pulled from the cookie/initialized and compare the original hash code to the hash code that exists just before I send the cookie header off to the client to ensure I don't have to re-serialize/send the cookie unless changes were made.</p>

<p>I was going to override the .NET's <code>Object.GetHashCode()</code> method, but I wasn't sure that this is the best way to go about checking if an object is modified.</p>

<p>Are there any other ways I can check if an object is modified, or should I override the <code>GetHashCode()</code> method.</p>

<p><strong>Update</strong> I decided to accept @rmbarnes's answer as it had an interesting solution to the problem, and because I decided to use his advice at the end of his post and not check for modification. I'd still be interested to hear any other solutions anyone may have to my scenario however.</p>

[{'answer_id': 34828, 'author': 'Craig Walker', 'author_id': 3488, 'author_profile': 'https://Stackoverflow.com/users/3488', 'pm_score': 2, 'selected': False, 'text': '<p>GetHashCode() should always be in sync with Equals(), and Equals() isn\'t necessarily guaranteed to check for <em>all</em> of the fields in your object (there\'s certain situations where you want that to not be the case).</p>\n\n<p>Furthermore, GetHashCode() isn\'t guaranteed to return unique values for all possible object states. It\'s conceivable (though unlikely) that two object states could result in the same HashCode (which does, after all, only have an int\'s worth of possible states; see <a href="http://en.wikipedia.org/wiki/Pigeonhole_principle" rel="nofollow noreferrer">the Pigeonhole Principle</a> for more details).</p>\n\n<p>If you can ensure that Equals() checks all of the appropriate fields, then you could possibly clone the object to record its state and then check it with Equals() against the new state to see if its changed.</p>\n\n<p>BTW: Your mention of serialization gave me an idea. You could serialize the object, record it, and then when you check for object changing, repeat the process and compare the serialized values. That would let you check for state changes without having to make any code changes to your object. However, this isn\'t a great solution, because:</p>\n\n<ol>\n<li>It\'s probably very inefficient</li>\n<li>It\'s prone to serialization changes in the object; you might get false positives on the object state change.</li>\n</ol>\n'}, {'answer_id': 34830, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': '<p>I personally would say go with the plan you have.. A good hash code is the best way to see if an object is "as-is".. Theres tons of hashing algorithms you can look at, check out the obvious <a href="http://en.wikipedia.org/wiki/Hash_function" rel="nofollow noreferrer">Wikipedia page</a> on hash functions and go from there..</p>\n\n<p>Override GetHashCode and go for it! Just make sure <em>ALL</em> the elements of the information make up part of the hash :)</p>\n'}, {'answer_id': 34882, 'author': 'Robin Barnes', 'author_id': 1349865, 'author_profile': 'https://Stackoverflow.com/users/1349865', 'pm_score': 2, 'selected': True, 'text': "<p>At the end of the object's constructor you could serialize the object to a base 64 string just like the cookie stores it, and store this in a member variable. </p>\n\n<p>When you want to check if the cookie needs recreating, re - serialize the object and compare this new base 64 string against the one stored in a member variable. If it has changed, reset the cookie with the new value.</p>\n\n<p>Watch out for the gotcha - don't include the member variable storing the base 64 serialization in the serialization itself. I presume your language uses something like a sleep() function (is how PHP does it) to serialize itself, so just make sure the member is not included in that function.</p>\n\n<p>This will always work because you are comparing the exact value you'd be saving in the cookie, and wouldn't need to override GetHashCode() which sounds like it could have nasty consequences.</p>\n\n<p>All that said I'd probably just drop the test and always reset the cookie, can't be that much overhead in it when compared to doing the change check, and far less likelyhood of bugs.</p>\n"}, {'answer_id': 38380, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 0, 'selected': False, 'text': "<p>Seems odd to me why you'd want to store the same object both server side and client side - especially if you're comparing them on each trip.</p>\n\n<p>I'd guess that deserializing the cookie and comparing it to the server side object would be equivalent in performance to just serializing the object again.</p>\n\n<p>But, if you wanted to do this, I'd compare the serialized server side object with the cookie's value and update accordingly. Worst case, you did the serialization for naught. Best case, you did a string compare.</p>\n\n<p>The alternative, deserializing and comparing the objects, has a worst case of deserializing, comparing n fields, and then serializing. Best case is deserializing and comparing n fields.</p>\n"}]
['https://Stackoverflow.com/questions/34809', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/392/']