I have a method which returns an object, which could be one of a number of different data types, including strings, numbers and bools; and at some point I need to compare the equality of two values returned from this method. I’m using == instead of Equals() because I need different number types to compare – ie. 3 == 3.0 – which is working fine for strings and numbers, but for some reason it falls down when I’m comparing boolean values.
What would be the best way to solve this problem? I’d prefer not to have to detect the type and cast if bool, but I will do if there is no other solution
My code looks something like:
private object GetValue() {
// does some stuff, returns either a boolean, string or number value
}
var value1 = GetValue();
var value2 = GetValue();
if (value1 == value2) {
// do something
}
1 Answer