如何声明一个泛型委托具有out参数声明、参数、out

2023-09-04 02:33:13 作者:绻影浮沉

Func键<一,出B,布尔> ,只是不编译,如何声明,我想第二个参数是吗?

我想用这样的:

 公共类Foo()
 {
     公共Func键<一,出B,布尔> DetectM​​ethod;
 }
 

解决方案

其实func是在.NET Framework宣布只是一个简单的委托。其实有几个函数功能的代表宣布有:

 委托TResult Func键< TResult>()
委托TResult Func键< T,TResult>(T obj)以
委托TResult Func键&所述; T1,T2,TResult>(T1 OBJ1,T2 obj2的)
委托TResult Func键&所述的T1,T2,T3,TResult>(T1 OBJ1,T2 obj2的,T3 obj3)
委托TResult Func键&所述; T1,T2,T3,T4,TResult>(T1 OBJ1,T2 obj2的,T3 obj3,T4 OBJ4)
 

所以,你唯一可以做的事情就是声明你自定义委托:

 委托布尔MYFUNC< T1,T2>(T1一,出T2 B)
 
买信托为什么要双录,双录对投资者来说有什么意义

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

解决方案

Actually Func is just a simple delegate declared in the .NET Framework. Actually there are several Func delegates declared there:

delegate TResult Func<TResult>()
delegate TResult Func<T, TResult>(T obj)
delegate TResult Func<T1, T2, TResult>(T1 obj1, T2 obj2)
delegate TResult Func<T1, T2, T3, TResult>(T1 obj1, T2 obj2, T3 obj3)
delegate TResult Func<T1, T2, T3, T4, TResult>(T1 obj1, T2 obj2, T3 obj3, T4 obj4)

So the only thing you can do is declare your custom delegate:

delegate bool MyFunc<T1, T2>(T1 a, out T2 b)