Don't mix up lifetime and scope.
The scope of the variable
img
is the method
fn
. From the code of this method one can see it's completely useless. You download the image and loose the reference to it.
Lifetime is a bit more complex matter. This is a reference object. When a reference becomes inaccessible in any part of currently running code, the object is marked for garbage collection. However, the destruction itself happens some time later, according to an internal algorithm used by the
Garbage Collector (GC). You can detect this moment if you write a destructor. However, you cannot control this point in time unless you explicitly call
System.GC.Collect
. As a rule of thumb, doing this is not recommended and you also don't need to write destructors unless you have to release some unmanaged resources.
One would say that the object referenced by a local variable only becomes inaccessible when the code goes out of the scope. Wrong!
The existence of anonymous methods makes this even more complex. Using a local variable in an anonymous method prolongs its life time. This is called a
closure, see
http://en.wikipedia.org/wiki/Closure_(computer_science)[
^].
Please see my past answers for the explanation:
Looping for creating new thread give some problem[
^],
What are Anonymous Types in C#?[
^].
—SA