How to declare a generic delegate with an out parameter

Benny picture Benny · Mar 15, 2010 · Viewed 17.6k times · Source

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

Answer

Andrew Bezzub picture Andrew Bezzub · Mar 15, 2010

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)
delegate TResult Func<T1, T2, ... , T16, TResult>(T1 obj1, T2 obj2, ..., T16 obj16)

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

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