"Simon Harvey" <si**********@the-web-works.co.uk> wrote in
news:u9**************@TK2MSFTNGP12.phx.gbl:
Hi Alex,
Thanks for your reply.
I would have thought that them being references would cause a problem.
For example, lets say you add multiple commands to a list, repeatedly
updating the same variable name as I demonstrated in the OP.
The variable by itself contains a reference.
Think of it this way.
When you construct the SqlCommand object, it gets placed at memory
location 1000. 1000 is then stored into cmd.
When you do myArrayList.Add(cmd), you add 1000 to this list. You don't
add a reference to the variable itself, you just copy the value it
contains.
Next you do it all over again, but since the memory location 1000 and
forward is still (probably) occupied by the old object, you get 1050 this
time, and now 1050 is stored in cmd. 1050 is then added to the list. Now
there is two such objects in memory, at location 1000 and 1050.
At this point, your cmd variable contains 1050 referencing that object,
and your list contains 1000 and 1050 referencing those two objects. The
old object can not be garbage collected as the list still has an existing
reference to it.
As long as you have a valid reference which the compiler can go through
in order to find your objects (in this case it has your list, which has
references to the objects) then the objects won't be garbage collected.
--
Lasse Vågsæther Karlsen
la***@vkarlsen.no
PGP KeyID: 0x0270466B