I am working on a method that needs to repeat a small operation in different places, but the code that needs to be repeated must be closed to the method. The obvious solution is a nested function. However, no matter what I try, the C # compiler is on me.
Something roughly equal to this Perl fragment:
my $method = sub { $helper_func = sub { code to encapsulate };
this is what I'm talking about and what I'm trying to accomplish in C #.
No other method in a class should have access to a helper function in this context. The most logical way to write this construct in C #, it seems to me, would be something like this:
var helper = (/* parameter names */) => { };
And actually make the compiler make money.
Since such an assignment is forbidden, like the equivalent using the syntax of the older delegate () {} instead of lambda, and therefore declares the delegate type in the method - what csc actually allows me to write, however, is this:
private delegate Helper(); private method() { Helper helper = () => { };
Thatβs all fine and dandy in order not to copy and paste a piece of code and edit the parameters manually, but it comforts the private type of the delegate for the rest of the class, and does not keep it private for the method. What strikes the target in the first place. Using goto operators and local variables for parameters would provide better encapsulation of the helper in this context without sacrificing code reuse. If I wanted to simulate function calls by passing parameters through registers, I would rather use assembler. I did not find an acceptable way to refactor the code to avoid the problem at all.
So, is it even possible to make this Common Object Oriented Language obey?