Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compare two objects of unknown types (including reference and value types)

Tags:

c#

.net

Is it possible in C# to compare two objects of unknown types, (including both reference and value types) using their type comparators if they exist?

The goal is to write a function that would have a signature like this:

public bool Compare(object a, object b)
{
     // compare logic goes here
}

Which would return

Compare(100d, 100d) == true
Compare(100f, 100f) == true
Compare("hello", "hello") == true
Compare(null, null) == true 
Compare(100d, 101d) == false
Compare(100f, null) == false

// Use type comparators where possible, i.e.:
Compare(new DateTime(2010, 12, 01), new DateTime(2010, 12, 01)) == true
Compare(new DateTime(2010, 12, 01), new DateTime(2010, 12, 02)) == false
Compare(new DateTime(2010, 12, 01), null) == false

Is there a generic approach to solving this problem that would work for any type of object?

like image 543
Duane Avatar asked Dec 02 '10 07:12

Duane


3 Answers

You can use the static object.Equals(object x, object y) method and not bother writing your method at all. That will handle nulls appropriately, and delegate to an implementation of object.Equals(object) associated with either x or y... it shouldn't matter which, as Equals is meant to be symmetric.

Note that this doesn't use the == operators for any type - operators can't be overridden, only overloaded (which means they're chosen at compile-time, not execution-time. In most cases Equals should do what you want. In some cases, == may not be overloaded even though Equals is overridden... but I've never known the reverse to be true in any types I've worked with.

Note that using this approach will box any value types...

EDIT: Removed section about effectively reimplementing - poorly - EqualityComparer<T>.Default. See Marc's answer for more. This won't help you if you can't use a generic type, of course.

One final point: I wouldn't call your method Compare. That name is usually associated with ordering values rather than comparing them for equality.

like image 101
Jon Skeet Avatar answered Sep 18 '22 07:09

Jon Skeet


A reasonable option is is to work with generics, i.e.

public bool Compare<T>(T a, T b) {...} 

You won't need to specify the T in your code, as the compiler will usually be able to figure it out (i.e. your existing samples would work "as-is")

For the implementation:

bool equal = EqualityComparer<T>.Default.Equals(x, y); 

(for generic type T)

but actually I would avoid the word Compare, as that is used elsewhere to mean < / == / > - so I might have:

public static bool Equals<T>(T a, T b) {     return EqualityComparer<T>.Default.Equals(a, b); } 

This:

  • avoids boxing when T is a struct
  • handles nulls / Nullable<T> correctly
  • supports IEquatable<T>
  • or falls back to regular Equals

it does not use the == operator, but MiscUtil has an Operator class that will, via

bool equal = Operator.Equal<T>(x,y); 

(note this latter will fail if T doesn't have an == operator, although thinking about it, it could use EqualityComparer<T>.Default.Equals as a fallback; it just doesn't)


For completeness, note that Comparer<T>.Default.Compare(x,y) handles comparison operations.

like image 33
Marc Gravell Avatar answered Sep 22 '22 07:09

Marc Gravell


How about object.equals(x, y)? This will accept null values as well.

like image 38
BlueVoodoo Avatar answered Sep 21 '22 07:09

BlueVoodoo