i need to compare two objects but compare a number of their properties in one hit.
this is not for sorting, but instead to confirm whether anything has changed; as one is the old saved instance, and the second is a newly imported instance of the same thing
i assume this is best served by writing a custom comparer. just am a bit confused as to whether to do IComparer, or IComparable, or what tbh.
thanks
nat
If you only have a single definition of equality for your class, you don’t really need to implement any interface: simply override the
Equalsmethod. Best practice though, would be to implementIEquatable<T>and to overrideGetHashCodesensibly (if you don’t override the hash-code, equality will misbehave when collection classes, LINQ methods etc. use it as a pre-condition for equality). Here’s a sample implementation:This will allow you to do:
If, on the other hand, you do have lots of different definitions of equality for different circumstances, your best bet would be to write up different
IEqualityComparer<T>implementations. Here’s a sample implementation:In this case, the check will look like: