Comparing Objects In C#

Introduction

In this article, we are going to compare two objects of the same class having collection property or multiple collection properties. We will create a generic compare method that will compare the two objects of the same class. This comparison will be applicable to any type of class.

It is quite easy to compare the objects having only primitive properties. The problem arises when we have collection properties (say List) as member property inside the class.

This compare method will be used to compare the objects having List properties.

Step 1

Create a console application (say ObjectComparasionDemo) in C# using Visual Studio and add classes as below. You can create these as a separate file also.

  1. public class Employee {  
  2.     public int EmpId {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string EmpName {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public int EmpAge {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public DateTime JoiningDate {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public List < Department > EmpDepartment {  
  19.         get;  
  20.         set;  
  21.     }  
  22. }  
  23. public class Department {  
  24.     public int DeptId {  
  25.         get;  
  26.         set;  
  27.     }  
  28.     public string DeptName {  
  29.         get;  
  30.         set;  
  31.     }  
  32. }  
  33. public class Student {  
  34.     public int StudentId {  
  35.         get;  
  36.         set;  
  37.     }  
  38.     public string StudentName {  
  39.         get;  
  40.         set;  
  41.     }  
  42.     public DateTime JoiningDate {  
  43.         get;  
  44.         set;  
  45.     }  
  46.     public List < Course > StudentCourse {  
  47.         get;  
  48.         set;  
  49.     }  
  50. }  
  51. public class Course {  
  52.     public int CourseId {  
  53.         get;  
  54.         set;  
  55.     }  
  56.     public string CourseName {  
  57.         get;  
  58.         set;  
  59.     }  

Step 2 - Add the static class CompareObject.cs

This method is responsible for comparing the given two objects. This will recursively call the same method when it encounters the List property inside an object. This will read all the properties of the object and compare it with the second object's property. When it encounters List property, it will start comparison on each and every object available inside its collection by recursively calling the same method.

  1. public static class CompareObject {  
  2.     public static bool Compare < T > (T e1, T e2) {  
  3.         bool flag = true;  
  4.         bool match = false;  
  5.         int countFirst, countSecond;  
  6.         foreach(PropertyInfo propObj1 in e1.GetType().GetProperties()) {  
  7.             var propObj2 = e2.GetType().GetProperty(propObj1.Name);  
  8.             if (propObj1.PropertyType.Name.Equals("List`1")) {  
  9.                 dynamic objList1 = propObj1.GetValue(e1, null);  
  10.                 dynamic objList2 = propObj2.GetValue(e2, null);  
  11.                 countFirst = objList1.Count;  
  12.                 countSecond = objList2.Count;  
  13.                 if (countFirst == countSecond) {  
  14.                     countFirst = objList1.Count - 1;  
  15.                     while (countFirst > -1) {  
  16.                         match = false;  
  17.                         countSecond = objList2.Count - 1;  
  18.                         while (countSecond > -1) {  
  19.                             match = Compare(objList1[countFirst], objList2[countSecond]);  
  20.                             if (match) {  
  21.                                 objList2.Remove(objList2[countSecond]);  
  22.                                 countSecond = -1;  
  23.                                 match = true;  
  24.                             }  
  25.                             if (match == false && countSecond == 0) {  
  26.                                 return false;  
  27.                             }  
  28.                             countSecond--;  
  29.                         }  
  30.                         countFirst--;  
  31.                     }  
  32.                 } else {  
  33.                     return false;  
  34.                 }  
  35.             } else if (!(propObj1.GetValue(e1, null).Equals(propObj2.GetValue(e2, null)))) {  
  36.                 flag = false;  
  37.                 return flag;  
  38.             }  
  39.         }  
  40.         return flag;  
  41.     }  

Step 3 - Modify the Main method of class/file Program.cs

For our convinience, we have created the objects of both Employee and Student, so as to demonstrate that this compare method will be applicable on both classes and you can create your own class for comparision.

  1. static void Main(string[] args) {  
  2.     bool compare;  
  3.     var datetime = DateTime.Now;  
  4.     Employee e1 = new Employee {  
  5.         EmpId = 1, EmpAge = 25, EmpName = "Subbu", JoiningDate = datetime, EmpDepartment = new List < Department > {  
  6.             new Department() {  
  7.                 DeptId = 1, DeptName = "android"  
  8.             },  
  9.             new Department() {  
  10.                 DeptId = 2, DeptName = "ios"  
  11.             }  
  12.         }  
  13.     };  
  14.     Employee e2 = new Employee {  
  15.         EmpId = 2, EmpAge = 26, EmpName = "Subbu", JoiningDate = datetime, EmpDepartment = new List < Department > {  
  16.             new Department() {  
  17.                 DeptId = 3, DeptName = "mvc"  
  18.             },  
  19.             new Department {  
  20.                 DeptId = 4, DeptName = "mvvm"  
  21.             }  
  22.         }  
  23.     };  
  24.     Employee e3 = new Employee {  
  25.         EmpId = 1, EmpAge = 25, EmpName = "Subbu", JoiningDate = datetime, EmpDepartment = new List < Department > {  
  26.             new Department() {  
  27.                 DeptId = 1, DeptName = "android"  
  28.             },  
  29.             new Department() {  
  30.                 DeptId = 2, DeptName = "ios"  
  31.             }  
  32.         }  
  33.     };  
  34.     compare = CompareObject.Compare < Employee > (e1, e2);  
  35.     if (compare) {  
  36.         Console.WriteLine("e1 and e2 Employee Objects are Equal");  
  37.     } else {  
  38.         Console.WriteLine("e1 and e2 Employees object are Not equal");  
  39.     }  
  40.     compare = CompareObject.Compare < Employee > (e1, e3);  
  41.     if (compare) {  
  42.         Console.WriteLine("e1 and e3 Employee Objects are Equal");  
  43.     } else {  
  44.         Console.WriteLine("e1 and e3 Employees object are Not equal");  
  45.     }  
  46.     compare = CompareObject.Compare < Employee > (e2, e3);  
  47.     if (compare) {  
  48.         Console.WriteLine("e2 and e3 Employee Objects are Equal");  
  49.     } else {  
  50.         Console.WriteLine("e2 and e3 Employees object are Not equal");  
  51.     }  
  52.     Student s1 = new Student {  
  53.         StudentId = 1, StudentName = "Imran", StudentCourse = new List < Course > {  
  54.             new Course() {  
  55.                 CourseId = 1, CourseName = "Course_A"  
  56.             },  
  57.             new Course() {  
  58.                 CourseId = 2, CourseName = "Course_B"  
  59.             }  
  60.         }  
  61.     };  
  62.     Student s2 = new Student {  
  63.         StudentId = 2, StudentName = "Irfan", StudentCourse = new List < Course > {  
  64.             new Course() {  
  65.                 CourseId = 3, CourseName = "Course_C"  
  66.             },  
  67.             new Course() {  
  68.                 CourseId = 2, CourseName = "Course_D"  
  69.             }  
  70.         }  
  71.     };  
  72.     Student s3 = new Student {  
  73.         StudentId = 1, StudentName = "Imran", StudentCourse = new List < Course > {  
  74.             new Course() {  
  75.                 CourseId = 1, CourseName = "Course_A"  
  76.             },  
  77.             new Course() {  
  78.                 CourseId = 2, CourseName = "Course_B"  
  79.             }  
  80.         }  
  81.     };  
  82.     compare = CompareObject.Compare < Student > (s1, s2);  
  83.     if (compare) {  
  84.         Console.WriteLine("s1 and s2 Student Objects are Equal");  
  85.     } else {  
  86.         Console.WriteLine("s1 and s2 Students object are Not equal");  
  87.     }  
  88.     compare = CompareObject.Compare < Student > (s1, s3);  
  89.     if (compare) {  
  90.         Console.WriteLine("s1 and s3 Student Objects are Equal");  
  91.     } else {  
  92.         Console.WriteLine("s1 and s3 Students object are Not equal");  
  93.     }  
  94.     compare = CompareObject.Compare < Student > (s2, s3);  
  95.     if (compare) {  
  96.         Console.WriteLine("s2 and s3 Student Objects are Equal");  
  97.     } else {  
  98.         Console.WriteLine("s2 and s3 Students object are Not equal");  
  99.     }  
  100. }  

Execute the application. You will get the following output.

Output

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now