P
Peter Webb
Sorry about the stupid newbie questions, but some things aren't very well
explained in the doco ... at least that I can find.
I do something in my code that works just fine, trouble is I don't know why.
Here is is ..
namespace WindowsApplication1
{
public partial class Form1 : Form
{
static List<foo> foocollection;
..
..
..
}
private void Add_new_foo ()
{ foo foonew =new foo();
foocollection.Add(foonew);
}
Now when I call Add_new_foo, it creates foonew and adds it to foocollection,
as I want it to.
However, as I understand it this is just an object reference to foonew in
foocollection. But foonew only exists during Add_new_foo - its created on
the stack. Why doesn't it disappear when we exit Add_new_foo, and cause the
reference to foonew in foocollection to reference a deleted object? Is the
garbage collector/compiler/something clever enough to know there is a
reference to it in foocollection, and hence keep a persistent copy?
As I say, the code works, and operates how I want it to, but I don't
understand why.
Can anybody help explain this to me?
explained in the doco ... at least that I can find.
I do something in my code that works just fine, trouble is I don't know why.
Here is is ..
namespace WindowsApplication1
{
public partial class Form1 : Form
{
static List<foo> foocollection;
..
..
..
}
private void Add_new_foo ()
{ foo foonew =new foo();
foocollection.Add(foonew);
}
Now when I call Add_new_foo, it creates foonew and adds it to foocollection,
as I want it to.
However, as I understand it this is just an object reference to foonew in
foocollection. But foonew only exists during Add_new_foo - its created on
the stack. Why doesn't it disappear when we exit Add_new_foo, and cause the
reference to foonew in foocollection to reference a deleted object? Is the
garbage collector/compiler/something clever enough to know there is a
reference to it in foocollection, and hence keep a persistent copy?
As I say, the code works, and operates how I want it to, but I don't
understand why.
Can anybody help explain this to me?