Skip to main content
 首页 » 编程设计

c#之如何显式定义匿名方法符合什么签名

2024年02月20日17qq78292959

我声明了两个方法

public void MethodA(object o, Action<string> action) { } 
public void MethodA(object o, Action<CustomType> action) { } 

如何使用匿名方法调用这些函数?我知道我可以传递一个指向方法的指针,但我有兴趣使用匿名方法这样做吗?目前我收到错误消息“Ambitious call between...”

MethodA(this, c => { }); // how to explicitly say that C is of type CustomType? 

请您参考如下方法:

MethodA(this, (CustomType c) => { }); 

或者如果您想将委托(delegate)类型显式声明为 Action<CustomType> :

MethodA(this, (Action<CustomType>)(c => { }));