Share via


csharp comparing two objects Differences

Question

Sunday, October 2, 2016 6:21 AM

I am trying to compare 2 objects with properties and child properties. What is the best option to compare and show differences between objects in this case?

Student a = new Student();
a.Name = "john";
a.Address ="CA";
//a.StudDetails.Location = "LA";
//a.StudDetails.Study = "MBA";
//a.StudDetails.Friends = new string[] { "X", "Y"};

Student b = new Student();
b.Name = "john";
b.Address = "CA";
//b.StudDetails.Location = "LA";
//b.StudDetails.Study = "MBA";
//b.StudDetails.Friends = new string[] { "X", "Y"};
bool x = Equals(a, b);
if (x)
{
    Console.WriteLine("matched");
}
else
{
    Console.WriteLine("Not Matched");
}


public class Student
{
    public string Name;
    public Details StudDetails;
    public string Address;

public override bool Equals(Object obj) 
  {
  // Check for null values and compare run-time types.
  if (obj == null || GetType() != obj.GetType()) 
   return false;

  Student s = (Student)obj;
  return (Name == s.Name) && (Address == s.Address);
  }

} public class Details { public string Study; public string Location; public string[] Friends; }

All replies (2)

Sunday, October 2, 2016 10:04 AM âś…Answered

Hi Chan,

chandrashekarthota

I am trying to compare 2 objects with properties and child properties. What is the best option to compare and show differences between objects in this case?

According to your description, I suggest you could use reflection to enumerate through all of the properties and determine which are and are not equal, then return some list of properties and both differing values.

More details, you could refer to follow codes:

static void Main(string[] args)
        {
            Student a = new Student();
            a.Name = "john";
            a.Address = "CA";
            Details dt = new Details();
            dt.Study = "High";
            dt.Location = "A";
            a.StudDetails = dt;

            Student b = new Student();
            b.Name = "john";
            b.Address = "CB";
            Details dt1 = new Details();
            dt1.Study = "middle";
            dt1.Location = "B";
            b.StudDetails = dt1;
            List<Variance> rt = a.DetailedCompare(b);
        }
   
        public class Student
        {
            public string Name;
            public Details StudDetails;
            public string Address;
        }
        public class Details { public string Study; public string Location; public string[] Friends; }

        public static List<Variance> DetailedCompare<T>(this T val1, T val2)
        {
            List<Variance> variances = new List<Variance>();
            FieldInfo[] fi = val1.GetType().GetFields();
            foreach (FieldInfo f in fi)
            {
                Variance v = new Variance();
                v.Prop = f.Name;
                v.valA = f.GetValue(val1);
                v.valB = f.GetValue(val2);
                if (!v.valA.Equals(v.valB))
                    variances.Add(v);
            }
            return variances;
        }
       public class Variance
        {
            public string Prop { get; set; }
            public object valA { get; set; }
            public object valB { get; set; }
        }

Result:

Best Regards,

Brando


Sunday, October 2, 2016 9:34 PM

If two objects of type Student are equal when all three properties are the same then compare all three properties.

return (name == s.Name) && (Address == s.Address) && (StudDetails == s.StudDetails);

(Don't forget to override == and GetHashCode() as well)

If two objects of type Detail are equal when all properties are the same then compare all properties.