Important refs question

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi all,

When executing the following, is it a copy or a reference thats copied to
the array?

SqlCommand cmd = new SqlCommand("w", x);
myArrayList.Add(cmd); // A reference
or a copy?


The reason i ask is that I then want to go on and use the same variable name
to make a new cmd and then copy that to the array as well

eg:

SqlCommand cmd = new SqlCommand("y", z);
myArrayList.Add(cmd); // A reference
or a copy?


Is this possible? It does actually seem to work but I don't think it should.

Should I make a copy of the object before putting it in the array list

Many thanks

Simon
 
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.

When you come to access the command, the references will only point to the
last object. The rest will have no references to them and may be garbage
collected at any moment

Is this not the case

Thanks again

Simon
 
Simon,

this is not the case - you can think about references as of variables, which
store addresses of objects. So, changing reference by assigning new value
(new object instance address) you might lose old one. But in your case old
reference is kept in array list as item, so while array list item exists
corresponding reference exists too. At the same time cmd will keep only
reference to last object created.

HTH
Alex
 
Simon Harvey said:
I would have thought that them being references would cause a problem.

No, because the value of the variable is the reference. When you assign
a new value to the variable, you've lost the connection with the
previous value.
For example, lets say you add multiple commands to a list, repeatedly
updating the same variable name as I demonstrated in the OP.

When you come to access the command, the references will only point to the
last object. The rest will have no references to them and may be garbage
collected at any moment

Is this not the case

Not if you create a new object each time. There's a difference between:

Foo foo = new Foo();
for (...)
{
foo.SomeProperty = someValue;
someList.Add(foo);
}

and

Foo foo;;
for (...)
{
foo = new Foo();
foo.SomeProperty = someValue;
someList.Add(foo);
}
 
Hi Alex,

Simon Harvey said:
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.

When you come to access the command, the references will only point to the
last object. The rest will have no references to them and may be garbage
collected at any moment

Is this not the case

No, they will be referenced by the ArrayList's internal list . that's why
they are not collected

Cheers,
 
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.
 
Back
Top