chugh97
chugh97

Reputation: 9984

Func delegate with ref variable

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

How do I define a Func delegate for this method?

Upvotes: 81

Views: 42241

Answers (2)

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31641

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

Upvotes: 10

Elisha
Elisha

Reputation: 23770

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

Upvotes: 123

Related Questions