why this return 5,I’m curious about this the normal function have higher priority over with a default parameter one. can some body explain me this. thanks in advance
static void Main(string[] args)
{
Console.WriteLine(add());
Console.ReadLine();
}
public static int add(int x=4,int y=5)
{
return x+y;
}
public static int add()
{
return 5;
}
I think this can only be answered by looking at the C# language specification. Section 7.5.3.2 says that when determining a “Better” function member, if the optional arguments are not needed, you throw them out:
That effectively makes the two functions equivalent. It then has the following tie-breaker rule for when two functions are equivalent:
That would mean that the one without the default arguments would be the one called – just as you’re seeing in your code.