【发布时间】:2019-01-29 05:45:59
【问题描述】:
C# 语言规范6.5 匿名函数转换 指出:
...
具体来说,匿名函数 F 与提供的委托类型 D 兼容:
...
如果 F 不包含 匿名函数签名,那么 D 可能有 零个或多个任意类型的参数,只要 D 的参数没有 out 参数修饰符。
但是,以下代码会产生错误。
using System;
namespace DelegateAnonymousFunctionExample
{
public delegate void D(int i, int b);
class Program
{
static void Main(string[] args)
{
// Valid
D f1 = (int a, int b) =>
{
Console.WriteLine("Delegate invoked...");
};
f1(3, 4);
// Error
D f2 = () =>
{
Console.WriteLine("Delegate invoked...");
};
Console.ReadKey();
}
}
}
我在上面的代码中哪里出错了?
【问题讨论】:
-
你能提供这个规范的链接吗?
-
microsoft.com/en-us/download/details.aspx?id=7029。这是一个下载链接,不知道有没有在线版本。
-
错误是什么?
-
@John ''Delegate 'D' does not take 0 arguments"
标签: c# delegates anonymous-function