views:

53

answers:

2

Consider the following (C#) code. The lambda being passed to ConvolutedRand() is said to be "closed over" the variable named format. What term would you use to describe how the variable random is used within MyMethod()?

void MyMethod
{
    int random;
    string format = "The number {0} inside the lambda scope";

    ConvolutedRand(x =>
    {
        Console.WriteLine(format, x);
        random = x;
    });

    Console.WriteLine("The number is {0} outside the lambda scope", random);
}

void ConvolutedRand(Action<int> action)
{
    int random = new Random.Next();
    action(random);
}
+1  A: 

That would be a local variable IMO. (Perhaps there is a more scientific name, not free maybe?)

leppie
i can tell that you're qualified to answer this question based on your user icon.
anthony
+3  A: 

I typically hear "bound" versus "free", in the context of a particular expression or lexical scope. The lambda closes over both format and random (which are 'free' in the lambda, which is why it closes over them). Inside MyMethod, both variables are just locally bound variables.

Brian