1

10.Equals(10f) result in false while 10 == 10f results in true. Latter is because when comparing an int with a float the int is converted to float. I just don't understand why int.Equals obviously does not convert the int value into float if the passed object is a float object.

This in my opinion is a problem with constant matching, when assuming that variable == constant is identical to variable is constant. If the variable is of type object, for example following matching fails:

object value = 10f;
if (value is 10)
{
    // This block is not run
}

I would not expect this match to fail. So, why is it that 10.Equals(10f) result in false?

Jürgen Bayer
  • 2,993
  • 3
  • 26
  • 51
  • 3
    See [C# Equals() == about number](https://stackoverflow.com/questions/46884638/c-sharp-equals-about-number). – dxiv Jul 07 '20 at 16:53
  • 1
    Check the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.int32.equals?view=netcore-3.1#System_Int32_Equals_System_Object_) the part *"Note to callers"* – Cid Jul 07 '20 at 16:56
  • Thanks for the info. Still confusing with constant pattern matching, but that's how it is I assume. – Jürgen Bayer Jul 07 '20 at 17:08

0 Answers0