使用返回void类型的新的函数功能< T,TResult> [英] Using void return types with new Func<T, TResult>

查看:210
本文介绍了使用返回void类型的新的函数功能< T,TResult>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用匿名委托在我的代码调用这个函数的例子:

 公共静态INT TestFunction(int类型的, INT b){
返回A + b;
}



该委托是这样的:

  VAR德尔=新Func键与LT; INT,INT,INT>(TestFunction); 



我的问题是:你如何指定无效 TResult ?以下不工作:

 公共静态无效OtherFunction(int类型的,字符串B){...} 
VAR德尔=新Func键与LT; INT,字符串无效>(OtherFunction);


解决方案

如果没有返回类型,你想动作< INT,串>

  VAR德尔=新动作< INT,字符串>(OtherFunction); 



或者只是:

 动作< INT,串>德尔= OtherFunction; 


I'm using an anonymous delegate in my code calling this example function:

public static int TestFunction(int a, int b) {
    return a + b;
}

The delegate looks like this:

var del = new Func<int, int, int>(TestFunction);

My question is: how do you specify a void return type for TResult? The following doesn't work:

public static void OtherFunction(int a, string b) { ... }
var del = new Func<int, string, void>(OtherFunction);

解决方案

If there is no return type, you want Action<int,string>:

var del = new Action<int, string>(OtherFunction);

or just:

Action<int, string> del = OtherFunction;

这篇关于使用返回void类型的新的函数功能&LT; T,TResult&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆