Sophisticated object comparison in C #

I have two complex objects of the same type. I want to compare both objects to determine if they have the same value. What is an effective way to do this?

The structure of the class of samples shown below:

class Package
{
    public List<GroupList> groupList;
}
class GroupList
{
   public List<Feature> featurelist;
}
class Feature
{
   public int qty;
}
+3
source share
8 answers

Good, so you want to make a deep, disordered structural comparison. The "disordered" part is complex, and in fact it is a strong hint that your classes are not designed correctly: List<T>inherently ordered, so you might prefer to use it HashSet<T>there (if you don't, expect duplicates). This will make the comparison easier and faster (although the inserts will 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();
    }
}

List<T>, LINQ - , , :

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();
    }
}
+5

, . IComparable Equals.

+2

, , "" .

GetHashCode, , . , , " " .

, , - / . " " .

, ... .

+2
+1
+1

, , , IComparer.

, , :

  • , . , , , false
  • If you can implement an efficient hash code, you can first compare the hashes, if they are not equal, then the objects are not equal, if they are equal, then you need to compare the objects to see if the objects are equal

So, first make the fastest comparisons to try and return false.

+1
source

Here is a slightly simplified way to do this using reflection. You will probably need to add other data type checks for specific comparisons or scrolling lists, etc., but this should help get you started.

void Mymethod(){

Class1 class1 = new Class1();

//define properties for class1

Class1 class2 = new Class1();
//define properties for class2

PropertyInfo[] properties = class1.GetType().GetProperties();

bool bClassesEqual = true;

foreach (PropertyInfo property in properties)
{
    Console.WriteLine(property.Name.ToString());

    if (property.GetValue(class1, null) != property.GetValue(class2, null))
    {
        bClassesEqual = false;
        break;

    }

}

}

0
source

Source: https://habr.com/ru/post/1725436/


All Articles