Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two integer objects for equality regardless of type

Tags:

c#

.net

math

boxing

I'm wondering how you could compare two boxed integers (either can be signed or unsigned) to each other for equality.

For instance, take a look at this scenario:

// case #1 object int1 = (int)50505; object int2 = (int)50505; bool success12 = int1.Equals(int2); // this is true. (pass)  // case #2 int int3 = (int)50505; ushort int4 = (ushort)50505; bool success34 = int3.Equals(int4); // this is also true. (pass)  // case #3 object int5 = (int)50505; object int6 = (ushort)50505; bool success56 = int5.Equals(int6); // this is false. (fail) 

I'm stumped on how to reliably compare boxed integer types this way. I won't know what they are until runtime, and I can't just cast them both to long, because one could be a ulong. I also can't just convert them both to ulong because one could be negative.

The best idea I could come up with is to just trial-and-error-cast until I can find a common type or can rule out that they're not equal, which isn't an ideal solution.

like image 934
caesay Avatar asked Jan 26 '16 06:01

caesay


People also ask

How do you compare two integer values?

Syntax : public static int compare(int x, int y) Parameter : x : the first int to compare y : the second int to compare Return : This method returns the value zero if (x==y), if (x < y) then it returns a value less than zero and if (x > y) then it returns a value greater than zero.

How do you compare equality of objects?

The behavior for performing loose equality using == is as follows: If the operands have the same type, they are compared as follows: Object: return true only if both operands reference the same object. String: return true only if both operands have the same characters in the same order.

Can you use == to compare integers?

To compare integer values in Java, we can use either the equals() method or == (equals operator). Both are used to compare two values, but the == operator checks reference equality of two integer objects, whereas the equal() method checks the integer values only (primitive and non-primitive).

How do you compare two integers equal in Java?

To check two numbers for equality in Java, we can use the Equals() method as well as the == operator. Firstly, let us set Integers. Integer val1 = new Integer(5); Integer val2 = new Integer(5); Now, to check whether they are equal or not, let us use the == operator.

What is the difference between an integer and an object?

Integer is a value type. When you compare two integers types, compiller checks their values. Object is a reference type. When you compare two objects, compiller checks their references. Compiller perfoms boxing operation, wraps value type into reference type, and Equals will compare references, not values.

How to compare two integer objects in Java?

Integer objects are objects. This sounds logical, but is the answer to the question. Objects are made in Java using the new keyword, and then stored in the memory. When comparing, you compare the memory locations of the objects, not the value/properties of the objects. Using the .equals() method,...

How to check for reference equality between two objects?

First example: Using the == operator between objects checks for reference equality, and since you are comparing two different objects they do not equal.

What is the difference between compiller's equals and object comparison?

When you compare two integers types, compiller checks their values. Object is a reference type. When you compare two objects, compiller checks their references. Compiller perfoms boxing operation, wraps value type into reference type, and Equals will compare references, not values.


1 Answers

In case 2, you actually end up calling int.Equals(int), because ushort is implicitly convertible to int. This overload resolution is performed at compile-time. It's not available in case 3 because the compiler only knows the type of int5 and int6 as object, so it calls object.Equals(object)... and it's natural that object.Equals will return false if the types of the two objects are different.

You could use dynamic typing to perform the same sort of overload resolution at execution time - but you'd still have a problem if you tried something like:

dynamic x = 10; dynamic y = (long) 10; Console.WriteLine(x.Equals(y)); // False 

Here there's no overload that will handle long, so it will call the normal object.Equals.

One option is to convert the values to decimal:

object x = (int) 10; object y = (long) 10; decimal xd = Convert.ToDecimal(x); decimal yd = Convert.ToDecimal(y); Console.WriteLine(xd == yd); 

This will handle comparing ulong with long as well.

I've chosen decimal as it can exactly represent every value of every primitive integer type.

like image 101
Jon Skeet Avatar answered Oct 03 '22 01:10

Jon Skeet