GetHashCode () returns Int32 (not MD5).
If you create two objects with all the same property values, they will not have the same hash if you use the base or system GetHashCode ().
String is an object and an exception.
string s1 = "john"; string s2 = "john"; if (s1 == s2) returns true and will return the same GetHashCode()
If you want to control the comparison of equality of two objects, you must override GetHash and Equality.
If two objects are the same, then they must also have the same GetHash (). But two objects with the same GetHash () are not necessarily the same. The comparison will check GetHash () first, and if it succeeds there, it will check Equals. Well, there are a few comparisons that go straight to Equals, but you should still redefine both and make sure that two identical objects produce the same GetHash.
I use this to synchronize the client with the server. You can use all properties or you can change any change to the VerID property. The advantage here is the faster GetHashCode () accelerator. In my case, I already reset VerID with any property change.
public override bool Equals(Object obj) { //Check for null and compare run-time types. if (obj == null || !(obj is FTSdocWord)) return false; FTSdocWord item = (FTSdocWord)obj; return (OjbID == item.ObjID && VerID == item.VerID); } public override int GetHashCode() { return ObjID ^ VerID; }
I ended up using ObjID so that I can do the following
if (myClientObj == myServerObj && myClientObj.VerID <> myServerObj.VerID) {
Object.GetHashCode Method
Two objects with the same property values. Are they equal? Do they produce the same GetHashCode ()?
personDefault pd1 = new personDefault("John"); personDefault pd2 = new personDefault("John"); System.Diagnostics.Debug.WriteLine(po1.GetHashCode().ToString()); System.Diagnostics.Debug.WriteLine(po2.GetHashCode().ToString()); // different GetHashCode if (pd1.Equals(pd2)) // returns false { System.Diagnostics.Debug.WriteLine("pd1 == pd2"); } List<personDefault> personsDefault = new List<personDefault>(); personsDefault.Add(pd1); if (personsDefault.Contains(pd2)) // returns false { System.Diagnostics.Debug.WriteLine("Contains(pd2)"); } personOverRide po1 = new personOverRide("John"); personOverRide po2 = new personOverRide("John"); System.Diagnostics.Debug.WriteLine(po1.GetHashCode().ToString()); System.Diagnostics.Debug.WriteLine(po2.GetHashCode().ToString()); // same hash if (po1.Equals(po2)) // returns true { System.Diagnostics.Debug.WriteLine("po1 == po2"); } List<personOverRide> personsOverRide = new List<personOverRide>(); personsOverRide.Add(po1); if (personsOverRide.Contains(po2)) // returns true { System.Diagnostics.Debug.WriteLine("Contains(p02)"); } } public class personDefault { public string Name { get; private set; } public personDefault(string name) { Name = name; } } public class personOverRide: Object { public string Name { get; private set; } public personOverRide(string name) { Name = name; } public override bool Equals(Object obj) { //Check for null and compare run-time types. if (obj == null || !(obj is personOverRide)) return false; personOverRide item = (personOverRide)obj; return (Name == item.Name); } public override int GetHashCode() { return Name.GetHashCode(); } }
paparazzo
source share