函数没有参数

发布于 2024-08-02 04:20:43 字数 327 浏览 12 评论 0原文

我可以将带有 out 参数的方法作为 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。

有没有办法做到这一点?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃→可爱长大的 2024-08-09 04:20:43

refout 不是类型参数定义的一部分,因此您不能使用内置 Func 委托来传递 refout 参数。当然,如果您愿意,您可以声明自己的委托:

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

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);
我纯我任性 2024-08-09 04:20:43

为什么不创建一个类来封装结果呢?

public class Result
{
     public IList<Foo> List { get; set; }
     public Int32 Count { get; set; }
}

Why not create a class to encapsulate the results?

public class Result
{
     public IList<Foo> List { get; set; }
     public Int32 Count { get; set; }
}
本宫微胖 2024-08-09 04:20:43

委托的 Func 系列(或就此而言的 Action)只不过是声明为

//.NET 4 and above
public delegate TResult Func<out TResult>()
public delegate TResult Func<in T, out TResult>(T obj)

//.NET 3.5
public delegate TResult Func<T1, T2, TResult>(T1 obj1, T2 obj2)
public delegate TResult Func<T1, T2, T3, TResult>(T1 obj1, T2 obj2, T3 obj3)

等的简单委托类型。这样的委托可以具有 out/ref 参数,所以在您的情况下正如其他答案所指出的那样,这只是您自己自定义实现的问题。 至于为什么微软没有默认打包这个,想想它需要的组合数量之多。

delegate TResult Func<T1, T2, TResult>(T1 obj1, T2 obj2)
delegate TResult Func<T1, T2, TResult>(out T1 obj1, T2 obj2)
delegate TResult Func<T1, T2, TResult>(T1 obj1, out T2 obj2)
delegate TResult Func<T1, T2, TResult>(out T1 obj1, out T2 obj2)

仅两个参数。 我们甚至没有触及ref。 对于开发人员来说,这实际上会很麻烦且令人困惑。

The Func family of delegates (or Action for that matter) are nothing but simple delegate types declared like

//.NET 4 and above
public delegate TResult Func<out TResult>()
public delegate TResult Func<in T, out TResult>(T obj)

//.NET 3.5
public delegate TResult Func<T1, T2, TResult>(T1 obj1, T2 obj2)
public delegate TResult Func<T1, T2, T3, TResult>(T1 obj1, T2 obj2, T3 obj3)

etc. Delegates as such can have out/ref parameters, so in your case its only a matter of custom implementation by yourself as other answers have pointed out. As to why Microsoft did not pack this by default, think of the sheer number of combinations it would require.

delegate TResult Func<T1, T2, TResult>(T1 obj1, T2 obj2)
delegate TResult Func<T1, T2, TResult>(out T1 obj1, T2 obj2)
delegate TResult Func<T1, T2, TResult>(T1 obj1, out T2 obj2)
delegate TResult Func<T1, T2, TResult>(out T1 obj1, out T2 obj2)

for just two parameters. We have not even touched ref. It would actually be cumbersome and confusing for developers.

面如桃花 2024-08-09 04:20:43

您可以将其包装在公开正确接口并调用 FindForBar 的 lambda/delegate/function/method 中,但我怀疑 FindForBar 已将其计为 out 参数作为原因,因此您需要确保丢弃该信息ok/safe/desirable/有正确的结果(即使您可以直接传入 FindForBar,您也需要确定这一点)。

You could wrap it in a lambda/delegate/function/method that exposed the right interface and called FindForBar, but I suspect that FindForBar has count as an out parameter as a reason, so you'd need to be sure throwing that information away was ok/safe/desirable/had the right results (you'd need to be sure of this even if you could just directly pass in FindForBar).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文