Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a generic delegate with an out parameter [duplicate]

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;  } 
like image 548
Benny Avatar asked Mar 15 '10 14:03

Benny


People also ask

Can delegates be used with generic parameters?

Delegates defined within a generic class can use the generic class type parameters in the same way that class methods do.

How do you pass a parameter to a delegate?

Use Action Delegate to Pass a Method as a Parameter in C# We can also use the built-in delegate Action to pass a method as a parameter. The correct syntax to use this delegate is as follows. Copy public delegate void Action<in T>(T obj); The built-in delegate Action can have 16 parameters as input.

What is out _ in C#?

The out is a keyword in C# which is used for the passing the arguments to methods as a reference type. It is generally used when a method returns multiple values.


1 Answers

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) 
like image 50
Andrew Bezzub Avatar answered Sep 19 '22 09:09

Andrew Bezzub