Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Func delegate with ref variable

public object MethodName(ref float y) { //method } 

How do I defined a Func delegate for this method?

like image 500
chugh97 Avatar asked Mar 17 '10 14:03

chugh97


People also ask

What is func delegate in C#?

C# - Func Delegate Func is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter.

What is the difference between func string string and delegate?

Func is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter. This delegate can point to a method that takes up to 16 Parameters and returns a value.

What is TResult in C#?

TResult. The type of the return value of the method that this delegate encapsulates. This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.

How do you return a value from a delegate?

The key here is using the += operator (not the = operator) and looping through the list that is retrieved by calling GetInvocationList() and then calling Invoke() on each delegate retrieved. Hope this helps!


1 Answers

It cannot be done by Func but you can define a custom delegate for it:

public delegate object MethodNameDelegate(ref float y); 

Usage example:

public object MethodWithRefFloat(ref float y) {     return null; }  public void MethodCallThroughDelegate() {     MethodNameDelegate myDelegate = MethodWithRefFloat;      float y = 0;     myDelegate(ref y); } 
like image 51
Elisha Avatar answered Sep 24 '22 02:09

Elisha