I want to compare two objects without implementing the Equals() method.
What are the downsides of comparing them in this way: 1. serilizing them with Json 2. comparing the results
thanks!
I want to compare two objects without implementing the Equals() method.
What are the downsides of comparing them in this way: 1. serilizing them with Json 2. comparing the results
thanks!
Loss of speed. Transforming objects into JSON strings and then comparing them is much slower than doing a property by property equals.
Implementing
Equals()
is always the best way to compare two objects for equality.There is some overhead to the serialization process to convert objects to json. You'd have to test to see if the overhead is acceptable for your situation.
That aside, the source of the json object is a concern. I've seen a couple different json serializers format objects differently (e.g. quoting property names vs. not quoting them). Things like this could yield you untrue results.
Maybe with a class like that you can do the work (BsonDocument is a class from MongoDBDriver):
The downside is that you need to serialize them, which is potentially slow, and definitely slower than implementing
Equals
.You may also end up with part of the objects that you need to compare not being seriazlied and therefore not getting true comparison.