Resolving Ambiguities with Optional Parameters and Named Arguments

后端 未结 3 746
北恋
北恋 2021-01-23 07:36

I have two methods on my project as defined below:

void Person(int ID, double height = 0.0, string team = \"Knights\")
{
   //my codes
}
void Person(int ID, d         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-23 08:11

    If possible, the one which can be applied without default parameters is called.

    In the first case

    Person(1, 2.5, "Dark Ghost");
    

    First method is called.

    In the second case:

    Person(1, 46.5);
    

    It will simply result in build error. "The call is ambiguous between the following methods or properties: Test.Person(int, double, string) and Person(int, double, string, int)".

提交回复
热议问题