功能 T没有参数 [英] Func<T> with out parameter

查看:30
本文介绍了功能 T没有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将带有 out 参数的方法作为 Func 传递吗?

Can I pass a method with an out parameter as a Func?

public IList<Foo> FindForBar(string bar, out int count) { }

// somewhere else
public IList<T> Find(Func<string, int, List<T>> listFunction) { }

Func 需要一个类型,所以 out 不会在那里编译,调用 listFunction 需要一个 int 并且不允许 out in.

Func needs a type so out won't compile there, and calling listFunction requires an int and won't allow an out in.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

refout 不是类型参数定义的一部分,所以你不能使用内置的-in Func 委托传递 refout 参数.当然,如果你愿意,你可以声明你自己的委托:

ref and out are not part of the type parameter definition so you can't use the built-in Func delegate to pass ref and out arguments. Of course, you can declare your own delegate if you want:

delegate V MyDelegate<T,U,V>(T input, out U output);

这篇关于功能 T没有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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