[Solved] ArrayList of object references


It is already 2, as Array/Collections (to be precise any .NET Class/reference type) are passed by reference by default.

In fact the reference variable is passed by value, but behaves as if passed by reference.

Why ->

  Consider var arr = new ArrayList();

The above statement first creates an ArrayList object and a reference is assigned to arr. (This is similar for any Class as class are reference type).

Now at the time of calling,

example ->    DummyMethod(arr) , 

the reference is passed by value, that is even if the parameter is assigned to a different object within the method, the original variable remains unchanged.
But as the variable points(refer) to same object, any operation done on underlying pointed object is reflected outside the called method.
In your example, any modification done in for each will be reflected in the arrayList.

If you want to avoid this behavior you have to create copy/clone of the object.

Example:

Instead of

foreach (Foo x in dummyfoo)
        x.dummy++;

Use

foreach (Foo x in (ArrayList)dummyfoo.Clone())
        x.dummy++;

solved ArrayList of object references