Reputation: 41118
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);
}
Upvotes: 1
Views: 125
Reputation: 118895
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.
Upvotes: 3
Reputation: 117280
That would be a local variable IMO. (Perhaps there is a more scientific name, not free
maybe?)
Upvotes: 1