Assume that I have these two overloaded functions.
public static void Main(string[]args)
{
int x=3;
fn(x);
}
static void fn(double x)
{
Console.WriteLine("Double");
}
static void fn(float x)
{
Console.WriteLine("Float");
}
Why will the compiler choose the float function?
It follows the rules of section 7.5.3.2 of the C# 4 spec.
intis implicitly convertible to bothfloatanddouble, so both candidate methods are applicable. However, the conversion frominttofloatis “better than” the conversion frominttodoubleaccording to section 7.5.3.2-7.5.3.5:Here, there’s an implicit conversion from
floattodouble, but no implicit conversion fromdoubletofloat– sofloatis a better conversion target thandouble.