Compare 2 Objects of Generic Class Type in C#

Introduction

Usually we can compare 2 objects easily if we already know the class type and properties. But I will explain how to compare 2 objects of generic class type without knowing their properties in this article.

Step 1: Create a console application and add class Student with the properties as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Compare2Objects
{
    public class Program
    {
        static void Main(string[] args)
        {
        }
    }
    public class Student
    {
        public string Name { get; set; }
        public int StudentId { get; set; }
        public int Age { get; set; }
    }
}

Step 2: Add objects for students as below in the Main method.

Student Student1 = new Student() { Name = "Jack", Age = 15, StudentId = 100 };
Student Student2 = new Student() { Name = "Smith", Age = 15, StudentId = 101 };
Student Student3 = new Student() { Name = "Smith", Age = 15, StudentId = 101 };
Student Student4 = new Student() { Name = "Smit", Age = 15, StudentId = 102 };
Student Student5 = new Student() { Name = null, Age = 15, StudentId = 100 };

Step 3: Now I want to compare these above student objects with each other. I have added the following method inside the "Program" class.

static bool Compare<T>(T Object1, T object2)
{
     //Get the type of the object
     Type type = typeof(T);

     //return false if any of the object is false
     if (Object1 == null || object2 == null)
        return false;

    //Loop through each properties inside class and get values for the property from both the objects and compare
    foreach (System.Reflection.PropertyInfo property in type.GetProperties())
    {
         if (property.Name != "ExtensionData")
         {
             string Object1Value = string.Empty;
             string Object2Value = string.Empty;
             if (type.GetProperty(property.Name).GetValue(Object1, null) != null)
                   Object1Value = type.GetProperty(property.Name).GetValue(Object1, null).ToString();
             if (type.GetProperty(property.Name).GetValue(object2, null) != null)
                   Object2Value = type.GetProperty(property.Name).GetValue(object2, null).ToString();
             if (Object1Value.Trim() != Object2Value.Trim())
             {
                 return false;
             }
         }
    }
    return true;
}

The above method will get properties of the class and compare the values for each property. If any of the values are different, it will return false, else it will return true.

Step 4: So I am going to compare the objects by calling this method from the Main method as below.

if (Compare<Student>(Student1, Student2))
      Console.WriteLine("Both objects are equal");
else
     Console.WriteLine("Both objects are not equal");
if (Compare<Student>(Student3, Student2))
     Console.WriteLine("Both objects are equal");
else
     Console.WriteLine("Both objects are not equal");
if (Compare<Student>(Student3, Student4))
     Console.WriteLine("Both objects are equal");
else
     Console.WriteLine("Both objects are not equal");
if (Compare<Student>(Student1, Student5))
     Console.WriteLine("Both objects are equal");
else
     Console.WriteLine("Both objects are not equal");
     Console.Read();

The output will be: 

Both objects are not equal
Both objects are equal
Both objects are not equal
Both objects are not equal

For the complete source code, please find the attached solution.


Similar Articles