Accessing “Outer” Variables
Anonymous methods are interesting in that they are able to access the local variables of the method
that defines them. Formally speaking, such variables are termed
outer variables of the anonymous
method.
■
Note
An anonymous method cannot access
ref
or
out
parameters of the defining method.
Assume our Main() method defined a local integer named aboutToBlowCounter. Within the
anonymous methods that handle the AboutToBlow event, we will increment this counter by 1 and
print out the tally before Main() completes:
static void Main(string[] args)
{
...
int aboutToBlowCounter = 0;
Do'stlaringiz bilan baham: |