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!
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.
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.
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.
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.
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With