Func Delegate with Ref Variable

Func delegate with ref variable

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

Func / Action delegates with reference arguments/parameters or anonymous functions

You need to define a new delegate type for this method signature:

delegate void RefAction<in T>(ref T obj);

public void F()
{
RefAction<int> f2 = DoSomething;
int x = 0;
f2(ref x);
}

The reason why the .NET Framework does not include this type is probably because ref parameters are not very common, and the number of needed types explodes if you add one delegate type for each possible combination.

Passing the variable through delegate with ref parameter

Have found the solution:

        public delegate void DGraphInit(ref DataMedia media);
...
if (obj_mdata.state == GraphState.Running)
{
object[] data = { obj_mdata };
DGraphInit delegate2call = new DGraphInit(GraphInit);
this.Dispatcher.Invoke(delegate2call, data);
}

out / ref parameter for delegate

You may be mixing up C# "by reference" with C++ "by reference".

In your code, query is passed by reference, which means that the reference to the value of query is passed by value. Thus, changing query will change the value the reference is referencing. However, changing the reference itself does nothing.

query is immutable - there's no way to change the value. You can only create a new query, that contains the old query within itself. And that's exactly what e.g. OrderBy does - it doesn't change the query. This is one of the core features of LINQ and similar functional approaches in C# - mutable code is generally much harder to deal with in a general way, so you want to avoid it, especially on interfaces.

So what you need to do is pass the reference by reference, not by value. That's exactly what you've done by providing the Wrapper class. It's also possible to use ref keyword to do this, but it's completely unnecessary and rather hard to deal with in your case. ref only really makes sense with value types, though there are useful cases even for reference types; they are quite rare, though.

But the best and easiest approach is to simply follow the simple principle: don't change anything, just return an object that contains the change. Make your delegate return the query, rather than modifying the argument:

delegate IQueryable<...> YourDelegate(IQueryable<...> query);

IQueryable<...> YourMethod(IQueryable<...> query)
{
return query.OrderBy(...);
}

Delegate with ref parameter

If you mean without declaring the delegate type, then probably not; very few (if any) inbuilt delegates use ref; but you could make it generic:

delegate void ActionRef<T>(ref T value);

I'm not sure this saves much though. There may also be some tricks here with extension methods, but it is hard to tell without more detail.

C#. Ref returning delegate for ref extension method

A ref parameter is something you can only use within your function, not something you can "save for later". That's because the ref parameter cannot keep its referent variable alive.

Consider using a reference type (C# parlance: class) to hold your data. Then you can have any number of handles to it, and they'll participate in garbage collection (object lifetime, memory compaction, etc). Unfortunately, this requires changing all users of the variable -- it's not something you can retrofit onto data in an existing object model.

Delegate for an Action ref T1, T2

Create your own delegate type:

delegate void MyAction(ref DataRow dataRow, double doubleValue);

And use MyAction in place of Action<ref DataRow, Double> -- which, as you've noted, doesn't compile.



Related Topics



Leave a reply



Submit