开发者

Optional parameters on delegates doesn't work properly [duplicate]

This question already has answers here: Can a Delegate have an optional parameter? (2 answers) Closed 9 years 开发者_JS百科ago.

Why this piece of code does not compile?

delegate int xxx(bool x = true);

xxx test = f;

int f()
{
   return 4;
}


Optional parameters are for use on the calling side - not on what is effectively like a single-method-interface implementation. So for example, this should compile:

delegate void SimpleDelegate(bool x = true);

static void Main()
{
    SimpleDelegate x = Foo;
    x(); // Will print "True"
}

static void Foo(bool y)
{
    Console.WriteLine(y);
}


What will happen test(false)? It will corrupt the stack, because signatures must match.


Try this way:

static int f(bool a)
{
  return 4;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜