-1

I need to do something similar with command pattern, but in terms of delegate or something similar.

It should look like this:

private MyFunc Method1() {
    MyFunc func;
    
    /*
        set all parameters to func
    */  
    
    return func;
}

private void Method2()
{
    var funcWithAllParameters = Method1();
    funcWithAllParameters.Invoke();
}

private MyFunc(a lot of parameters) {}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Isn't it better to encapsulate MyFunc inside a class? The parameters could be the data members of the class. This way you can control their values or give default ones – Marco Beninca May 02 '22 at 10:37
  • You can't both return a `MyFunc` delegate that has parameters and then call it without specifying those parameter values. Instead, return a different, parameterless, delegate that matches the return type of `MyFunc`, something like either `Action` (for `void`) or `Func` for other return types. – Lasse V. Karlsen May 02 '22 at 11:01

2 Answers2

0

You can do this using a lambda function, which supplies the parameters for and calls MyFunc. The result is an Action delegate, which has the signature void ()

private Action Method1() {
    return () => MyFunc(new Type1(), null);
}

private void Method2()
{
    var funcWithAllParameters = Method1();
    funcWithAllParameters.Invoke();
}

private void MyFunc(Type1 p1, Type2 p2)
{
    //
}

dotnetfiddle

Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • I think currying is applying the parameters one by one. Like making func) => func>. As for the question use closures. Cf https://csharpindepth.com/articles/Closures#:~:text=To%20put%20it%20very%20simply,re%20going%20to%20be%20used. – Clemens May 02 '22 at 10:26
  • Closure is when you pull in a value from outside the lambda. I'm not doing that here: the values are created within the lambda. You might be right about currying, not sure if it's used to do multiple arguments simultaneously – Charlieface May 02 '22 at 10:29
  • So when is new Type1() evaluated? I did not consider this thoughtfully. If it is evaluated on calling Method1 (which I assumed in the first place - probably incorrectly) then it is added in the closure. As for the argument I think it would be nessesary to put some value in the closure and not create a value lazily. – Clemens May 02 '22 at 10:34
  • 1
    It is not. It is evaluated at runtime of the lambda, which is when you call `Method2`. A closure would be `var x1 = new Type1(); return () => MyFunc(x1, null);` then it would be evaluated during `Method1` – Charlieface May 02 '22 at 10:35
0

You can put the values into a closure like so:

private Func<TResult> Method1()
     => () =>  MyFunc(/*a lot of parameters*/"param1", 12, ...);

Thus you could load the parameters in the closure and use the Function afterwards:

var func = Method1();
var result = func();

And of course you can also use the function as parameter for other functions:

private TResult SomeOtherMethod(Func<TResult> functionWithSetParameters)
    => functionWithSetParameters();

 
Clemens
  • 588
  • 6
  • 9