FUNC< T>与出参数参数、FUNC、LT、GT

2023-09-02 21:21:22 作者:山川皆无恙

我可以通过一个方法的输出参数为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) { }

函数功能需要一个类型,这样出来不会编译那里,并呼吁listFunction需要一个int,也不允许在了。

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?

推荐答案

REF 退出不是的一部分类型参数的定义,所以你不能使用内置的 Func键委托传递 REF 退出参数。当然,你可以声明自己的代理,如果你想要的:

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);