Collection classes

G

Guest

Are there any collection classes that will take object references to that if
I add an object (could be primitive data types) to it and I change it through
the original object, the one in the collection classes will be changed
automatically too?
The ArrayList would make a copy of the original and therefore would not
reflect the change.
 
V

Vadym Stetsyak

ArrayList would make a copy of value type.
If reference type will be added to arraylist then only reference to the
object is stored, that is:

SomeObj someObj;
arrayList.Add(someObj);

//change the object
someObj.SomeProp = newValue;

//then
SomeObj newObj = arraList[index];

newObj and someObj will be equal thus their inner state will be the same.
newObj.SomeProp equals to newValue

For value types you have to get access to the value with the help of
container methods.
e.g.

int i = 5;
arrayList.Add(5);
i = 6;

int newOne = (int)arraList[index];
newOne will be 5;

that is why you have to do the action through a wrapper e.g.
arrayList.SetAt(index, i)
 

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