如何声明带有输出参数的通用委托
Func
,只是不编译,如何声明我希望第二个参数是out
1?
我想这样使用它:
public class Foo()
{
public Func<a, out b, bool> DetectMethod;
}
Func<a, out b, bool>
, just don't compile, how to declare that i want the second parameter be an out
one?
I want to use it like this:
public class Foo()
{
public Func<a, out b, bool> DetectMethod;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,Func 只是 .NET Framework 中声明的一个简单委托。实际上,那里声明了几个 Func 委托:
所以你唯一能做的就是声明你的自定义委托:
Actually, Func is just a simple delegate declared in the .NET Framework. Actually, there are several Func delegates declared there:
So the only thing you can do is declare your custom delegate:
您需要创建自己的委托类型,如下所示:
You need to make your own delegate type, like this:
您可能想重新考虑您的设计。您真的需要通过添加输出参数来使代码复杂化吗?
您可以将 bool 返回类型和第二个输出类型包装在它们自己的类(或 .NET 4.0 Tuple)中,并将其用作返回类型:
当然,当您想使用委托来引用 try-parse 方法时,您可以这样做正确的轨道,您需要定义一个新的委托,正如其他人已经描述的那样。
You might want to rethink your design. Do you really need to complicate your code by adding an out parameter?
You can wrap the bool return type and the second out type in their own class (or .NET 4.0 Tuple) and use that as a return type:
Of course when you want to use the delegates to reference try-parse methods, you are on the right track and you'll need to define a new delegate as others already described.