Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare the content of two objects for equality

Tags:

c#

I have two complex (i.e. objects with string, int, double, List and other home made data type) objects of the same type. I would like to compare the content of both of them to ensure that they are identical. Note: The object doesn't implement .Equals (I have no control on that) and doesn't implement IComparable.

Is there a generic way (reflection?) to compare the content of the two objects?

Thanks!

like image 311
Martin Avatar asked Dec 17 '08 20:12

Martin


People also ask

How do you compare equality of objects?

The behavior for performing loose equality using == is as follows: If the operands have the same type, they are compared as follows: Object: return true only if both operands reference the same object. String: return true only if both operands have the same characters in the same order.

How do you compare two objects in a set?

The equals() method of java. util. Set class is used to verify the equality of an Object with a Set and compare them. The method returns true if the size of both the sets are equal and both contain the same elements.

What is the difference between == and === while comparing objects?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.


1 Answers

I have created a class to perform a deep compare of .NET Objects. See:

https://github.com/GregFinzer/Compare-Net-Objects

like image 66
Greg Finzer Avatar answered Oct 13 '22 21:10

Greg Finzer