PART I
C h a p t e r 1 5 :
D e l e g a t e s , E v e n t s , a n d L a m b d a E x p r e s s i o n s
413
PART IPART I
}
}
The output is shown here. Pay special attention to the summation value.
0
1
2
3
Summation of 3 is 6
0
1
2
3
4
5
Summation of 5 is 21
As you can see, the count still proceeds normally. However, notice the summation value for
5. It shows 21 instead of 15! The reason for this is that
sum
is captured by
ctObj
when it is
created by the
Counter( )
method. This means it remains in existence until
count
is subject
to garbage collection at the end of the program. Thus, its value is not destroyed when
Counter( )
returns or with each call to the anonymous method when
Do'stlaringiz bilan baham: |