I have two complex objects like Object1 and Object2. They have around 5 levels of child objects.
I need the fastest method to say if they are same or not.
How could this be done in C# 4.0?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Implement
IEquatable<T>(typically in conjunction with overriding the inheritedObject.EqualsandObject.GetHashCodemethods) on all your custom types. In the case of composite types, invoke the contained types’Equalsmethod within the containing types. For contained collections, use theSequenceEqualextension method, which internally callsIEquatable<T>.EqualsorObject.Equalson each element. This approach will obviously require you to extend your types’ definitions, but its results are faster than any generic solutions involving serialization.Edit: Here is a contrived example with three levels of nesting.
For value types, you can typically just call their
Equalsmethod. Even if the fields or properties were never explicitly assigned, they would still have a default value.For reference types, you should first call
ReferenceEquals, which checks for reference equality – this would serve as an efficiency boost when you happen to be referencing the same object. It would also handle cases where both references are null. If that check fails, confirm that your instance’s field or property is not null (to avoidNullReferenceException) and call itsEqualsmethod. Since our members are properly typed, theIEquatable<T>.Equalsmethod gets called directly, bypassing the overriddenObject.Equalsmethod (whose execution would be marginally slower due to the type cast).When you override
Object.Equals, you’re also expected to overrideObject.GetHashCode; I didn’t do so below for the sake of conciseness.Update: This answer was written several years ago. Since then, I’ve started to lean away from implementing
IEquality<T>for mutable types for such scenarios. There are two notions of equality: identity and equivalence. At a memory representation level, these are popularly distinguished as “reference equality” and “value equality” (see Equality Comparisons). However, the same distinction can also apply at a domain level. Suppose that yourPersonclass has aPersonIdproperty, unique per distinct real-world person. Should two objects with the samePersonIdbut differentAgevalues be considered equal or different? The answer above assumes that one is after equivalence. However, there are many usages of theIEquality<T>interface, such as collections, that assume that such implementations provide for identity. For example, if you’re populating aHashSet<T>, you would typically expect aTryGetValue(T,T)call to return existing elements that share merely the identity of your argument, not necessarily equivalent elements whose contents are completely the same. This notion is enforced by the notes onGetHashCode: