Duane
Duane

Reputation: 700

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

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?

Upvotes: 48

Views: 22635

Answers (5)

Jon Skeet
Jon Skeet

Reputation: 1500065

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.

Upvotes: 75

Marc Gravell
Marc Gravell

Reputation: 1062600

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.

Upvotes: 21

BlueVoodoo
BlueVoodoo

Reputation: 3676

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

Upvotes: 6

KBoek
KBoek

Reputation: 5976

I'm not 100% sure if this works in all cases, but give it a try

public bool Compare(object a, object b)
{
    return a.Equals(b);
}

Upvotes: -4

Zeemee
Zeemee

Reputation: 10704

What about

if (a == null && b == null) return true;
if (a == null && b != null) return false;
return (a.equals(b));

?

Upvotes: 0

Related Questions