Introduction
In this article, we are going to compare two objects of the same class having collection properties 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 properties 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.
using System;
using System.Collections.Generic;
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public int EmpAge { get; set; }
public DateTime JoiningDate { get; set; }
public List<Department> EmpDepartment { get; set; }
}
public class Department
{
public int DeptId { get; set; }
public string DeptName { get; set; }
}
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public DateTime JoiningDate { get; set; }
public List<Course> StudentCourse { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
// Create an employee
Employee employee = new Employee
{
EmpId = 1,
EmpName = "John Doe",
EmpAge = 30,
JoiningDate = DateTime.Now,
EmpDepartment = new List<Department>
{
new Department { DeptId = 101, DeptName = "HR" },
new Department { DeptId = 102, DeptName = "IT" }
}
};
// Create a student
Student student = new Student
{
StudentId = 1001,
StudentName = "Alice Smith",
JoiningDate = DateTime.Now,
StudentCourse = new List<Course>
{
new Course { CourseId = 1, CourseName = "Math" },
new Course { CourseId = 2, CourseName = "Science" }
}
};
// Display information
Console.WriteLine("Employee Details:");
Console.WriteLine($"ID: {employee.EmpId}, Name: {employee.EmpName}, Age: {employee.EmpAge}");
Console.WriteLine("Departments:");
foreach (var department in employee.EmpDepartment)
{
Console.WriteLine($" - DeptID: {department.DeptId}, DeptName: {department.DeptName}");
}
Console.WriteLine("\nStudent Details:");
Console.WriteLine($"ID: {student.StudentId}, Name: {student.StudentName}");
Console.WriteLine("Courses:");
foreach (var course in student.StudentCourse)
{
Console.WriteLine($" - CourseID: {course.CourseId}, CourseName: {course.CourseName}");
}
}
}
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 the List property, it will start comparing each and every object available inside its collection by recursively calling the same method.


Satish Kumar VadlavalliPosted Dec 15, 2017, 5:53 AM
Nice article and thanks for sharing :)
Deepak GeraPosted Nov 30, 2017, 1:08 AM
This is good but compares only one level down lists. You can make it recursive to compare n level down.