Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:16:43+00:00 2026-06-03T07:16:43+00:00

I have two complex objects like Object1 and Object2 . They have around 5

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:16:44+00:00Added an answer on June 3, 2026 at 7:16 am

    Implement IEquatable<T> (typically in conjunction with overriding the inherited Object.Equals and Object.GetHashCode methods) on all your custom types. In the case of composite types, invoke the contained types’ Equals method within the containing types. For contained collections, use the SequenceEqual extension method, which internally calls IEquatable<T>.Equals or Object.Equals on 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 Equals method. 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 avoid NullReferenceException) and call its Equals method. Since our members are properly typed, the IEquatable<T>.Equals method gets called directly, bypassing the overridden Object.Equals method (whose execution would be marginally slower due to the type cast).

    When you override Object.Equals, you’re also expected to override Object.GetHashCode; I didn’t do so below for the sake of conciseness.

    public class Person : IEquatable<Person>
    {
        public int Age { get; set; }
        public string FirstName { get; set; }
        public Address Address { get; set; }
    
        public override bool Equals(object obj)
        {
            return this.Equals(obj as Person);
        }
    
        public bool Equals(Person other)
        {
            if (other == null)
                return false;
    
            return this.Age.Equals(other.Age) &&
                (
                    object.ReferenceEquals(this.FirstName, other.FirstName) ||
                    this.FirstName != null &&
                    this.FirstName.Equals(other.FirstName)
                ) &&
                (
                    object.ReferenceEquals(this.Address, other.Address) ||
                    this.Address != null &&
                    this.Address.Equals(other.Address)
                );
        }
    }
    
    public class Address : IEquatable<Address>
    {
        public int HouseNo { get; set; }
        public string Street { get; set; }
        public City City { get; set; }
    
        public override bool Equals(object obj)
        {
            return this.Equals(obj as Address);
        }
    
        public bool Equals(Address other)
        {
            if (other == null)
                return false;
    
            return this.HouseNo.Equals(other.HouseNo) &&
                (
                    object.ReferenceEquals(this.Street, other.Street) ||
                    this.Street != null &&
                    this.Street.Equals(other.Street)
                ) &&
                (
                    object.ReferenceEquals(this.City, other.City) ||
                    this.City != null &&
                    this.City.Equals(other.City)
                );
        }
    }
    
    public class City : IEquatable<City>
    {
        public string Name { get; set; }
    
        public override bool Equals(object obj)
        {
            return this.Equals(obj as City);
        }
    
        public bool Equals(City other)
        {
            if (other == null)
                return false;
    
            return
                object.ReferenceEquals(this.Name, other.Name) ||
                this.Name != null &&
                this.Name.Equals(other.Name);
        }
    }
    

    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 your Person class has a PersonId property, unique per distinct real-world person. Should two objects with the same PersonId but different Age values be considered equal or different? The answer above assumes that one is after equivalence. However, there are many usages of the IEquality<T> interface, such as collections, that assume that such implementations provide for identity. For example, if you’re populating a HashSet<T>, you would typically expect a TryGetValue(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 on GetHashCode:

    In general, for mutable reference types, you should override GetHashCode() only if:

    • You can compute the hash code from fields that are not mutable; or
    • You can ensure that the hash code of a mutable object does not change while the object is contained in a collection that relies on its hash code.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a unit test that wants to compare two complex for objects
Lets say you have various objects of arbitrary type that you would like to
I have two complex (i.e. objects with string, int, double, List and other home
So I have two custom complex types like this (oversimplified for this example): public
I have two lists comprised of different complex-objects, and each one is from 2
I have to compare two complex objects which are of different types (and do
I have two relatively complex queries that I am trying to join together into
I have two classes: Employee and EmployeeGridViewAdapter . Employee is composed of several complex
I have a complex JPA/Hibernate problem. I have two entities A and B. A
Trying to send a complex type between two systems that have the same code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.