tags:

views:

822

answers:

5

I have this code here, which is intended to allow any type of arguments:

public static void AreEqual(object expectedValue, object actualValue) {
 if (expectedValue == actualValue) {
  HttpContext.Current.Response.Write("Equal");
 } else {
  HttpContext.Current.Response.Write("Not Equal");
 }
}

If I call it using a couple of ints it does not behave very well.

AreEqual(3, 3)   // prints Not Equal
+13  A: 

At the simplest level:

public static void AreEqual(object expectedValue, object actualValue) {
    if (object.Equals(expectedValue,actualValue)) {
            HttpContext.Current.Response.Write("Equal");
    } else {
            HttpContext.Current.Response.Write("Not Equal");
    }
}

Or with generics (supports IEquatable<T>):

public static void AreEqual<T>(T expectedValue, T actualValue) {
    if (EqualityComparer<T>.Default.Equals(expectedValue,actualValue)) {
            HttpContext.Current.Response.Write("Equal");
    } else {
            HttpContext.Current.Response.Write("Not Equal");
    }
}
Marc Gravell
+1 for the generic alternative
Peter Lillevold
so does that mean you get like 10 populist badges at once? lol
Chad Grant
I don't have *any* populist badges ;-(. The "other" answer needs to be +10 too, I believe. So I guess you need +22 or someting!
Marc Gravell
+2  A: 

Try:

if (expectedValue.Equals(actualValue))

and of course you need to handle null, so you should try this:

Boolean eq = false;
if (expectedValue == null || actualValue == null)
    eq = (expectedValue == actualValue);
else
    eq = expectedValue.Equals(actualValue);

if (eq) {
    HttpContext.Current.Response.Write("Equal");
} else {
    HttpContext.Current.Response.Write("Not Equal");
}

This is of course the same as @mike nelson's answer:

if (Object.Equals(expectedValue, actualValue))

so go upvote his answer.

Lasse V. Karlsen
A: 
    if (expectedValue != null)
    {

     if (expectedValue.Equals(actualValue))
     {// enter code here
     }
}
Jean Azzopardi
That is a bad idea... what if expectedValue==null?
Marc Gravell
This must be the fastest downvoted accepted answer I've seen so far :)
Peter Lillevold
Sorry guys, I'll edit it.. I answered it too fast.
Jean Azzopardi
So happy I am sober and didn't accidentally leave a wrong answer on this one. wow
Chad Grant
+3  A: 

To check if the two object values are equal use this:

if (Object.Equals(expectedValue, actualValue)) {

As the normal == operator assumes an object is a reference type (despite the fact that value types also descend from objects).

mike nelson
I see what you did thar
Chad Grant
Why change the accepted answer then? =)
J. Steen
Why NOT change the accepted answer then, rather? ;)
J. Steen
There is no 'normal == operator'. In your method you've told the compiler to treat your arguments as the base object type. When you pass value types into your method, the value type is boxed and the object == operator does compare as references. Both 3's are boxed to different objects, and a reference equality check does correctly indicate the references are not equal.
Robert Paulson
You do not need to add the type prefix, Equals is good enough :)
leppie
mike nelson
+4  A: 

Just to highlight the reason for the "strange" behavior is because when you cast an int to object boxing occurs. The two 3s are converted to objects and then you are not comparing numbers anymore, you are comparing references, which will not be the same.

Peter Lillevold
+1 sarnath'd (why oh why 15 characters?)
annakata