0

If 2 variables of the type "object", which are Int32 and Int64, are compared, "true" is displayed in Visual Studio. If I save this in a variable, it suddenly changes to "false". I have already tried the EqualityComparer. The values remain unchanged.

int number1 = 2;
long number2 = 2;
object object1 = number1;
object object2 = number2;
var equals1 = number1 == number2; // true
var equals2 = object1 == object2; // object1 == object2 is true, equals2 is false

screenshot

Why is that?

EDIT:

updated screenshot

18
  • Simply, there's no easy way around this. When you do int == long, the compiler works out that there's a conversion from int to long, and inserts code to do this. When both sides are object, the compiler can't do this. There's nothing in the runtime which can take to integer types and figure out whether they can both be converted to a common type. If you know that one of the numbers is an int and the other is a long you can do this conversion yourself: Convert.ToInt64(object1) == Convert.ToInt64(object2) Commented Jan 4, 2022 at 16:44
  • I disagree with the dup because of the sentence "I have already tried the EqualityCompare" -- the EqualityComparer will already be doing what the answer in the dup says to do, and that doesn't work because OP's working with two boxed integer types which aren't necessarily the same type. Voting to re-open. Commented Jan 4, 2022 at 16:45
  • 1
    "Hardcast to Int32 / Int64 would unfortunately not be an option." -- can you explain why not? Understanding this will help us suggest a suitable workaround Commented Jan 4, 2022 at 16:47
  • @canton7 I have updated the screenshot regarding EqualityComparer. What confuses me is the fact that visual studio actually shows true when debugging if object1 and object2 are checked for equality. I don't really want a hardcast because I don't want to check out all the different type-combinations. However, if there is no elegant solution to this, I would have to consider that. Commented Jan 4, 2022 at 17:03
  • @Servy, again, that question doesn't address the issue of the boxed integers being different types. That's about <boxed int> == <boxed int>, not <boxed int> == <boxed long> Commented Jan 4, 2022 at 17:04

1 Answer 1

0

This is because you implicitly boxed the variables into the heap, which create new references, which is why equals2 is false.

Sign up to request clarification or add additional context in comments.

4 Comments

You can use Equals(object1, object2) to protect against null, but that doesn't solve the problem, see here
@canton7 right thats what == is doing but using the single arg version forces it to use the subtypes implementation.
I don't follow -- Equals(object a, object b) delegates to a.Equals(b) after null checks, see here. That doesn't solve the problem that Int32.Equals(object) doesn't attempt to do the built-in numeric conversions that the compiler will do if you use == between two integer types
object1.Equals(object2) isnt solving my problem. link

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.