8

This code works quite well in C# despite the fact that int can be implicitly converted to double and float:

void Main()
{
    int x = 7;
    F(x);
}
void F(double a)
{
    a.Dump("double");
}
void F(float a)
{
    a.Dump("float");
}

So, why this code won't compile? (The call is ambiguous between the following methods or properties: 'UserQuery.F(double)' and 'UserQuery.F(decimal)')

void Main()
{
    int x = 7;
    F(x);
}
void F(double a)
{
    a.Dump("double");
}
void F(decimal a)
{
    a.Dump("decimal");
}

All I did was replace the float variant of the function with a decimal variant.

user
  • 6,897
  • 8
  • 43
  • 79

1 Answers1

7

There are no implicit conversions between floating-point types and the decimal type.

If there's methods with float, double signatures there's no problem to determine a compatible method based on a minimum graduation requirements (the float method will be chosen).

There's no such rule when dealing with float-point types and decimal as there's no implicit priority between double and decimal and it must be specified explicitly.

shadeglare
  • 7,006
  • 7
  • 47
  • 59