Geek Culture
Published in

Geek Culture

How to compare two class objects?

Not just compared equality but the difference parts

We have experienced for comparing two object are equality for overriding the method “Object.Equals”. So that we could simple defined the own logic to comparing two object are equal or not?

// SomeClass.cs
public class SomeClass{
public string PropertyName1 {get; set;}
// ...

public override bool Equals(object obj){
var other = obj as SomeClass;
if (other == null){
return false;
if(PropertyName1 != other.PropertyName1){
return false;
return true;
}// in some function or block
var class1 = new SomeClass {PropertyName1 = "Name1"};
var class2 = new SomeClass {PropertyName1 = "Name2"};
// should return false
var requal = class1.Equals(class2);

The story we will talk more further. We would like to discuss how to comparing two distinct types of object and find out the difference between them. That’s suppose we have below classes:

public class ClassBase {
public string MESSAGE_ID { get; set; }
public string MSG_TYPE { get; set; } public string PO_NO { get; set; } public DateTime CREATE_TIME { get; set; }
// we have class1 inherit ClassBase
public class Class1 : ClassBase {
public string PO_LINE { get; set; }
public string GPN { get; set; } public string QTY { get; set; } public string PO_CHANGE_TYPE { get; set; } public string Price { get; set; }
// we have class2 inherit ClassBase
public class Class2 : ClassBase {
public string ICN { get; set; }
public string ICN_855_865 { get; set; } public string PO_TYPE { get; set; } public string PRODUCT_TYPE { get; set; } public string BLANKET_PO_BEGIN_DATE { get; set; } public string BLANKET_PO_END_DATE { get; set; } public string BILL_TO_COUNTRY_CODE { get; set; } public string PO_CHANGE_TYPE { get; set; } public string PO_LINE { get; set; } public string GPN { get; set; } public string FOCTORY_PART_NO { get; set; } public string VENDOR_PART_NO { get; set; } public string QTY { get; set; } public string UOM { get; set; } public string DELIVERY_REQ { get; set; } public bool Valid { get; set; }

We have marked the intersection properties as BOLD font.

So far the most thing we might care would be those intersection block were identity or with difference part ? We could comparing those two instances simply through below sample code via third-party library: ObjectsComparer.

// declare and initial class1
var class1 = new Class1 () {
PO_LINE = "1",
PO_NO = "12345678",
GPN = "ABC-123456",
QTY = "10",
MSG_TYPE = "850"
// declare and initial class2
var class2 = new Class2 () {
PO_LINE = "2",
PO_NO = "12345678",
GPN = "ABC-123456",
QTY = "20",
MSG_TYPE = "860",
ICN = "000000001"
// convert to expando object
var expandObj1 = JsonConvert.DeserializeObject<ExpandoObject> (JsonConvert.SerializeObject (class1));
var expandObj2 = JsonConvert.DeserializeObject<ExpandoObject> (JsonConvert.SerializeObject (class2));
var comparer = new ObjectsComparer.Comparer (new ComparisonSettings {
// null and empty error lists are equal
EmptyAndNullEnumerablesEqual = true
IEnumerable<Difference> differences;// compare and find the differences
var equal = comparer.Compare (expandObj1, expandObj2, out differences);

The result as shown below:

Result of Difference
Difference Result
🗝️ Intersection but value not consistency -> (A) [Yellow Block]
🗝️ Member(s) only in class1 -> (B) [Purple Block]
🗝️ Member(s) only in class2 -> (C) [Orange Block]

The key point here is that we convert our two object into “ExpandoObject” so that the library will take care the rest comparing process for us.


A new tech publication by Start it up (

Recommended from Medium

Collaborating vs Pairing

Building your own kubernetes CRDs

Stumbling to CTO: My Journey Becoming a Developer

Build an Alexa Skill in 7 Minutes Flat with Node.js and StdLib

Automated Versioning of Terraform Modules with GitHub Actions, SemVer Style

Steps to install Kubernetes in an on Premise Ubuntu server.

What they distribute: This ladies’ site is fundamentally, yet as well this

How to Fix Failed to Obtain IP Address? Here Are 5 Ways for You!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Coding for fun

More from Medium

Things you should know about .NET 5 Updated Version

[c++] unique_ptr incomplete type

Performance Wars — String Concatenation — C#

Deadly Diamond Problem in C#