我尝试在c#中检查方法重载 如下代码输出是21而不是22.它是否正确?
static void Main(string[] args)
{
double d = AddTwoNumbers(10 , 10);
Console.Write("Result : {0}", d);
Console.ReadKey();
}
static int AddTwoNumbers(int a, int b)
{
return a + b + 1;
}
static double AddTwoNumbers(double a, double b)
{
return a + b + 2;
}
答案 0 :(得分:0)
10
被视为integer
,而不是double
,除非您明确指定:
// using 'd' literal
double d = AddTwoNumbers(10d , 10d);
// Or via explicit cast
double d = AddTwoNumbers((double)10, (double)10);