Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Out" parameter (reference) with Action<>

Tags:

c#

.net

I have many methods similar to this:

 void GetColorSky(out float r, out float g, out float b)
 void GetColorGround(out float r, out float g, out float b)

"Similar" meaning they have the exact same method header excluding the method name. I also have several "colourPicker" controls, that accept R, G, B values.

I am trying to create a method that accepts a method as one of it's parameters, like so:

UpdateColourPicker(ColorPicker cp, CustomMethod cMethod)

and call it as follows:

UpdateColourPicker(cpSky, GetColorSky)
UpdateColourPicker(cpGround, GetColorGround)

What is the correct syntax for out and Action together? I've looked at this question but I still haven't managed.

Thanks!

like image 786
David Avatar asked Jun 12 '12 12:06

David


People also ask

Is out parameter in reference type?

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. The out parameter does not pass the property.

What is ref and out parameter?

ref is used to state that the parameter passed may be modified by the method. in is used to state that the parameter passed cannot be modified by the method. out is used to state that the parameter passed must be modified by the method.

What is an out parameter?

The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition​ as well as in the calling method.

What is an action parameter?

Action parameters enable you specify parameters that are available for all steps contained in the action. Action parameters are stored with the action and are maintained to all calls to the action. You can provide the action parameter value from any of the following: A test parameter.


2 Answers

Taking cue from that linked answer, this will work:

public delegate void GetColorDel(out float r, out float g, out float b);

void UpdateColourPicker(ColorPicker cp, GetColorDel cMethod) { }

UpdateColourPicker(cpSky, GetColorSky);
UpdateColourPicker(cpGround, GetColorGround);

Unfortunately, as stated in the answer to the question you link, Action and Func do not work with out and ref. This is simply because Action and Func are just delegate definitions stated (quite a few times to offer different overloads) with generics in the BCL. Adding ref and out variants would quickly cause re-definition compiler errors.

The full sample:

class Program
{        
    public delegate void GetColorDel(out float r, out float g, out float b);

    static void Main(string[] args)
    {
        UpdateColourPicker(null, GetColorSky);
        UpdateColourPicker(null, GetColorGround);

        Console.Read();
    }

    static void GetColorSky(out float r, out float g, out float b) 
    {
        r = g = b = 0f;
        Console.WriteLine("sky"); 
    }

    static void GetColorGround(out float r, out float g, out float b) 
    {
        r = g = b = 0f;
        Console.WriteLine("ground"); 
    }

    static void UpdateColourPicker(object cp, GetColorDel cMethod) 
    {
        float r, g, b;
        cMethod(out r, out g, out b);
    }
}
like image 149
Adam Houldsworth Avatar answered Oct 13 '22 20:10

Adam Houldsworth


If you want to define a generic delegate with out parameters, it should be done like this:

delegate void ActionWithOutparameter<T>(out T x);
delegate void ActionWithOutparameter<T1, T2>(out T1 x, out T2 y);
// ...

Clearly the Action and Func delegates in the BCL do not match this signature.

like image 22
jeroenh Avatar answered Oct 13 '22 20:10

jeroenh