The usage of "in" keyword doesnt let you modify the content of the
arraylist.
As far as valuetype such as int,bool are concerned, they might be
modifiable.
But when objects are added to the arraylist, the "in" keyword in forach,
returns a read-only reference, which you cant make it point to other
reference
One cant change the behaviour of "in", since its a keyword & not an operator
for eg
foreach (person p in alist)
p = new person("my name"); // this cant be done, because p is
read-only here (as the compiler says)
but you can do the following
foreach (person p in alist)
p.name = "new name";
If you want to store a new reference in alist
for(int i =0; i < alist.Count; i++)
alist
= new person("new name") // now this can be done, since it is
using subscript operator, which returns an object instance
I hope I answered, what you wanted. If anything is unclear, do write back on
the group
I am no expert though, but my understanding tells me this way to achieve it
HTH
Kalpesh
Lloyd Dupont said:
Does any of you have the slightest ideas of why you can't modify an array
list while in a foreach of its element.
I wrote my own collection and I'm trying to add the same behavior but I
don't know where to start.
I though to internal events but dismiss the idea because it prevents garbage
collection of the enumerator.
any other ideas ?