reference type

L

leshanster

I am quite new to C# (really .NET alltogether :) ), and I have a
question about referencing objects.

Say I have a class called node, and I have an ArrayList of nodes called
nodeList. I also have a function called getPos(string name) that given
the name of the node, it will return the position of the node in the
array.

now if I say:

node temp;
int i;

i = getPos(name);

temp = (node) nodeList;


will temp actually be a pointer to the node in the ArrayList, or will
temp have a copy of that particular node in the list? I am trying to
figure out if I manipulate temp, will I need to update the node in the
ArrayList, or is it already changed?

Thanks and sorry if this is an easy question :)
 
B

Bruce Wood

So long as node is a class, and not a struct, then (node)nodeList
will return you a reference to the node in the list. If you manipulate
the node, the node in the list will "also" change (because it's the
same node).

If node is a struct then you will get a copy, and you will have to
replace the node in the list with the modified copy if you want the
list to change. However, it's a questionable design decision to make
mutable structs. If you're a newbie, you probably want everything you
create to be a class, for now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top