0

I have two objects with the same type and values how can I compare them by value?

exp:

    class Person 
    {
    
    public string Name { get; set; }
    
    public DateTime BirthDate { get; set; }

    public Address Address { get; set; }
    
    }

class Address
    {
    
    public string City { get; set; }
    
    public int ZipCode { get; set; }
    
    }
    
    var p1 = new Person()
    {
    Name = "John doe",
    BirthDate = new DateTime(2000, 1, 1),
    Address = new Address(){
         City = "some city",
         ZipCode = 123456
    }
    };
    
    var p2 = new Person()
    {
    Name = "John doe",
    BirthDate = new DateTime(2000, 1, 1),
    Address = new Address(){
        City = "some city",
        ZipCode = 123456
    }
    };

so how can I compare these objects with value?

Mabey in future I wanna change my objects so I need a general way that not depends on object properties names and types

MM Hamzeh
  • 25
  • 7
  • I know I can convert my object to JSON and Compare them... I wanna know if there is a better way? – MM Hamzeh Dec 21 '20 at 10:17
  • IEquatable is great when I won't change my class in future – MM Hamzeh Dec 21 '20 at 10:17
  • 2
    You can do it with reflection: get all properties and check for them. In this way you can do what you want, for example iterate throw the full class tree (property of class type, like Address) and, if needed, you can create your custom attribute for exclude some properties from the check – PiGi78 Dec 21 '20 at 10:24
  • Does this answer your question? [How to quickly check if two data transfer objects have equal properties in C#?](https://stackoverflow.com/questions/986572/how-to-quickly-check-if-two-data-transfer-objects-have-equal-properties-in-c) – MichaelMao Dec 21 '20 at 11:01

2 Answers2

1

use json

Convert each object into a json string, with all properties/fields sorted by name, then compare two strings. this is slow, but it works.

use reflection

Using reflection methods, compare each property/field one by one. Actually the json library do the same job. Doing the comparison yourself will save the time converting to string, but you have to dive into the nested types.

use some code generator, e.g. protobuf

If the datatype is just POCO type, maybe protobuf is a good choice. It introduces a lot advantages:

  • build-in comparison
  • json serialization and deserialization
  • very fast binary serialization and deserialization
  • cross-platform and cross language, integrated well with grpc inter-process communication
  • version compatibility, when new fields added, old data on disk can still be read by app.
goldenbull
  • 93
  • 6
  • thanks, golden bull I, ve tried reflection and JSON as you say... both of them works correctly but using reflection done in a shorter time – MM Hamzeh Dec 23 '20 at 07:28
0

just make an "Equal function" in your Person class

public bool class Equals(Person source)
{
    if(this.Name!=source.Name) return false;
    if(this.Surname!=source.Surname)return false;
    ....
    return true;
}

Then use like that :

if(myFirstPerson.Equals(mySecondPerson))
{
    
}

Like that you can place as many as attributes as you want, then even make several Equals functions, if you need not to always compare the same attributes. Personally I always use this way (instead of "if values equal", I put "if value not equal then return false"), cause very useful when you have a lot of values to compare, and different equal functions.

Siegfried.V
  • 1,508
  • 1
  • 16
  • 34