可选参数,'Employee'的重载不会匹配委托'System.Func< Employee> [英] Optional Parameters, No overload for 'Employee' matches delegate 'System.Func<Employee>
问题描述
当试图调用一个引用带有可选参数的函数的委托时,iam获得
'Employee'匹配没有重载委托'System.Func
然后如何调用它,当我不需要传递参数时。
Func< Employee> print =员工;
Employee employee = print();
私人雇员雇员(int employeeID = 1)
{
return new Employee();
可选参数不包含更改方法签名。您试图将一个参数的方法转换为不带参数的委托 - 它们不兼容。
您可以使用一个lambda来委托您的方法使用可选参数的默认值:
Func< Employee> print =()=>雇员();
Employee employee = print();
When trying to invoke a delegate which reference a function with optional parameter, iam getting No overload for 'Employee' matches delegate 'System.Func
Then how to call it, when i don't need to pass the parameter.
Func<Employee> print = Employee;
Employee employee = print();
private Employee Employee(int employeeID = 1)
{
return new Employee();
}
The optional parameter doesn't change the method signature. You are trying to convert a method with one parameter to a delegate with no parameters - they are not compatible.
You can use a lambda that will delegate to your method making use of the default value for the optional parameter:
Func<Employee> print = () => Employee();
Employee employee = print();
这篇关于可选参数,'Employee'的重载不会匹配委托'System.Func< Employee>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!