Ste*_*nes 4 c# delegates optional-parameters
为什么这段代码不能编译?
delegate int xxx(bool x = true);
xxx test = f;
int f()
{
return 4;
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 13
可选参数用于主叫方 - 而不是有效的单方法接口实现.所以例如,这应该编译:
delegate void SimpleDelegate(bool x = true);
static void Main()
{
SimpleDelegate x = Foo;
x(); // Will print "True"
}
static void Foo(bool y)
{
Console.WriteLine(y);
}
Run Code Online (Sandbox Code Playgroud)