Func delegate with ref variable

C#C# 3.0ReferenceDelegatesFunc

C# Problem Overview


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

How do I define a Func delegate for this method?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

In .NET 4+ you can also support ref types this way...

public delegate bool MyFuncExtension<in string, MyRefType, out Boolean>(string input, ref MyRefType refType);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionchugh97View Question on Stackoverflow
Solution 1 - C#ElishaView Answer on Stackoverflow
Solution 2 - C#SliverNinja - MSFTView Answer on Stackoverflow