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 494405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:29:37+00:00 2026-05-13T05:29:37+00:00

I have two complex objects of the same type. I want to compare both

  • 0

I have two complex objects of the same type. I want to compare both the objects to determine if they have the exact same values. What is the efficient way of doing this ?

sample class structure given below:

class Package
{
    public List<GroupList> groupList;
}
class GroupList
{
   public List<Feature> featurelist;
}
class Feature
{
   public int qty;
}
  • 1 1 Answer
  • 1 View
  • 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-05-13T05:29:37+00:00Added an answer on May 13, 2026 at 5:29 am

    Okay, so you want deep unordered structural comparison. The “unordered” part is tricky, and in fact it is a strong hint that your classes are not designed right: List<T> is inherently ordered, so perhaps you would rather want to use a HashSet<T> there (if you don’t expect to have any duplicates). Doing so would make the comparison both easier to implement, and faster (though insertions would be slower):

    class Package
    {
        public HashSet<GroupList> groupList;
    
        public override bool Equals(object o)
        {
            Package p = o as Package;
            if (p == null) return false;
            return groupList.SetEquals(p.groupList);
        }
    
        public override int GetHashCode()
        {
            return groupList.Aggregate(0, (hash, g) => hash ^ g.GetHashCode());
        }
    }
    
    class GroupList
    {
       public HashSet<Feature> featureList;
    
        public override bool Equals(object o)
        {
            GroupList g = o as GroupList;
            if (g == null) return false;
            return featureList.SetEquals(g.featureList);
        }
    
        public override int GetHashCode()
        {
            return featureList.Aggregate(0, (hash, f) => hash ^ f.GetHashCode());
        }
    }
    
    class Feature
    {
        public int qty;
    
        public override bool Equals(object o)
        {
            Feature f = o as Feature;
            if (f == null) return false;
            return qty == f.qty;
        }
    
        public override int GetHashCode()
        {
            return qty.GetHashCode();
        }
    }
    

    If you want to keep using List<T>, you’ll need to use LINQ set operations – note, however, that those are significantly slower:

    class Package
    {
        public List<GroupList> groupList;
    
        public override bool Equals(object o)
        {
            Package p = o as Package;
            if (p == null) return false;
            return !groupList.Except(p.groupList).Any();
        }
    }
    
    class GroupList
    {
       public List<Feature> featureList;
    
        public override bool Equals(object o)
        {
            GroupList g = o as GroupList;
            if (g == null) return false;
            return !featureList.Except(f.featureList).Any();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to send a complex type between two systems that have the same code
Imagine I have two very complex but identical objects in c#, and I want
Say I have a unit test that wants to compare two complex for objects
I have two complex objects like Object1 and Object2 . They have around 5
I have two Generic Lists with the same objects Type T within them. For
I am currently working on comparing two complex objects of the same type, with
I have two complex (i.e. objects with string, int, double, List and other home
I have a situation where I have two arrays of objects. I want select
Synopsis: I have a need to take two generic C# objects, and if they
I have two relatively complex queries that I am trying to join together into

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.