Collection classes

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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)
 
Back
Top