Methods in C# - Outer Variables
(Page 2 of 4 )
A lambda expression can reference the local variables and parameters of the method in which it’s defined. For example:
delegate int NumericSequence ();
class Test
{
static void Main()
{
int seed = 0;
NumericSequence natural = () => seed++;
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 1
}
}
Local variables and parameters referenced by a lambda expression are called outer variables. In our example,seedis an outer variable referenced by the lambda expression() => seed++. Outer variables are captured, meaning their lifetime is extended to that of the lambda expression. Let’s refactor the example to make the effect of capturing more striking:
delegate int NumericSequence ();
class Test
{
static NumericSequence Natural ()
{
int seed = 0; // executes once (per call to Natural())
return () => seed++; // executes twice (per call to delegate instance
//
returned by Natural())
}
static void Main()
{
NumericSequence natural = Natural ();
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 1
}
}
The local variableseedwould ordinarily just pop off the stack when theNaturalmethod exits. However,seedis captured by the lambda expression of the delegate instance returned byNatural. This means the lifetime ofseedis extended to the lifetime of that delegate instance. Subsequent invocations of that same delegate instance will reuse the sameseed variable.
Capturing is internally implemented by “lifting” the captured variables into fields of a private class. When the method is called, the class is instantiated and lifetime-bound to the delegate instance.
A local variable instantiated within a lambda expression is unique per invocation of the delegate instance. If we refactor our previous example to instantiateseedwithin the lambda expression, we get a different (in this case, undesirable) result:
delegate int NumericSequence ();
class Test
{
static NumericSequence Natural ()
{
return () => {int seed = 0; return seed++; };
}
static void Main()
{
NumericSequence natural =
Natural ();
Console.WriteLine
(natural()); // 0
Console.WriteLine
(natural()); // 0
}
}
Next: Anonymous Methods >>
More C# Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.
|
|