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;
}
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 aHashSet<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):If you want to keep using
List<T>, you’ll need to use LINQ set operations – note, however, that those are significantly slower: