11

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)

2
  • declare it as a variable. Commented Sep 16, 2014 at 19:52
  • @DanielA.White I said it, when I tried it I got error, something really weird.
    – KugBuBu
    Commented Sep 16, 2014 at 19:52

3 Answers 3

30

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;
6
  • It could stop the confusing if microsoft could make Action action = () => action(); an exception. Thanks.
    – KugBuBu
    Commented Sep 16, 2014 at 19:59
  • @KugBuBu If they did it could allow someone to evaluate a variable that didn't yet have a value, which is something that they've gone out of their way to make impossible to do.
    – Servy
    Commented Sep 16, 2014 at 20:00
  • I mean to make it an exception. Just for this particular case.
    – KugBuBu
    Commented Sep 16, 2014 at 20:06
  • @KugBuBu And making that exception would allow you to write code that evaluated a variable to its value without that local variable having been assigned a value. As I said, the C# designers decided that they didn't want to make that possible.
    – Servy
    Commented Sep 16, 2014 at 20:07
  • @KugBuBu I highly recommend you go read "Minus 100 points" by Eric Gunnerson, a programmer for Microsoft. It explains very well why stuff like you are suggesting did not get put in. Commented Sep 16, 2014 at 20:09
4

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);
0

Here is an interesting post on the subject from the experts - http://blogs.msdn.com/b/wesdyer/archive/2007/02/02/anonymous-recursion-in-c.aspx

Excerpt from the post - "A quick workaround is to assign the value null to fib and then assign the lambda to fib. This causes fib to be definitely assigned before it is used.

Func<int, int> fib = null;
fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n;
Console.WriteLine(fib(6)); // displays 8

But our C# workaround doesn't really use recursion. Recursion requires that a function calls itself."

Read the entire post, if you are looking for other fun ways of doing it.

1
  • 1
    That most certainly is using recursion. The anonymous method most certainly is invoking itself. There's a layer of indirection there in that it's going through a delegate, but it's still happening.
    – Servy
    Commented Sep 16, 2014 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.