I am trying to make lambda able to reference to itself, an example:
PictureBox pictureBox=...;
Request(() => {
if (Form1.StaticImage==null)
Request(thislambda); //What to change to the 'thislambda' variable?
else
pictureBox.Image=Form1.StaticImage; //When there's image, then just set it and quit requesting it again
});
When I tried to put the lambda in variable, while the lambda referenced to itself, error of course.
I thought about creating class with a method that able to call itself, but I want to stick here with lambda. (While it gives only readibility so far and no advandges)
You need to declare the delegate, initialize it to something so that you are not accessing an uninitialized variable, and then initialize it with your lambda.
Action action = null;
action = () => DoSomethingWithAction(action);
Probably the most common usage I see is when an event handler needs to remove itself from the event when fired:
EventHandler handler = null;
handler = (s, args) =>
{
DoStuff();
something.SomeEvent -= handler;
};
something.SomeEvent += handler;
As of C# 7, you can also use local functions:
PictureBox pictureBox=...;
void DoRequest() {
if (Form1.StaticImage == null)
Request(DoRequest);
else
pictureBox.Image = Form1.StaticImage; //When there's image, then just set it and quit requesting it again
}
Request(DoRequest);
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