我刚刚看到以下代码:
class X
{
static Action Ac()
{
return ..some other code
}
}
这是什么意思?我从未见过宣布其主体的代表。
我刚刚看到以下代码:
class X
{
static Action Ac()
{
return ..some other code
}
}
这是什么意思?我从未见过宣布其主体的代表。
那不是Action声明其主体的代表。这是X类的静态方法,Ac()返回类型为Action; 也就是说,它是一个返回Action委托的类方法。主体可能会创建一个Action从该方法返回的对象。
换句话说:它是一个常规的静态方法,它恰好返回Action而不是像stringor之类的东西int。
引用匿名方法的委托对象声明如下(使用旧语法并省略 lambda 表示法):
Action<int> action = delegate (int x) {
//this is a body of anonymous method
//which is referenced by a delegate object action of type Action<int>
Console.WriteLine (x);
};
而不是像这样称呼:
action(5);