[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Lloyd Dupont

10/13/2003 4:42:00 AM

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 ?


8 Answers

Daniel O'Connell [C# MVP]

10/13/2003 6:47:00 AM

0


"Lloyd Dupont" <net.galador@ld> wrote in message
news:ea8V0QUkDHA.2268@TK2MSFTNGP12.phx.gbl...
> Does any of you have the slightest ideas of why you can't modify an array
> list while in a foreach of its element.
>
Because that is the rules when it comes to enumerators, it makes enumerator
implementation alot easier. Allowing changes requires a more sophisticated,
thread-safe enumerator. The probability of a bug in that case would be very
large in deed.
> 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 ?
>
Well, simple method, stick a private field like int version; in your class
and increment it on every change, following your proper locking rules and
methodologies of course(you may need to use
System.Threading.Interlocked.Increment in this case, assuming multiple
threads, enumerators, and processors). Then on every command to the
enumerator check that value, if it changed since the enumerator was created,
your enumerator is invalid and should throw an exception as specified in the
IEnumerable documentation.


>


Daniel O'Connell [C# MVP]

10/13/2003 7:05:00 AM

0


"Lloyd Dupont" <net.galador@ld> wrote in message
news:ea8V0QUkDHA.2268@TK2MSFTNGP12.phx.gbl...
> 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 ?
>
I forgot to mention, you could probably use internal events. foreach will
call IDisposable.Dispose if the enumerator implements IDisposable, so you
could use that call to clean up, its not as clean but it would work.
>


Kalpesh Shah

10/13/2003 10:22:00 AM

0

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[i] = 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" <net.galador@ld> wrote in message
news:ea8V0QUkDHA.2268@TK2MSFTNGP12.phx.gbl...
> 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 ?
>
>




Lloyd Dupont

10/13/2003 11:40:00 AM

0

I like this int version inside a list, but if my list is gong to be used in
multithreaded environment, ain't all these lock() {} going to be a
performance hit ?

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> a écrit dans le message
de news:fTrib.550641$Oz4.497665@rwcrnsc54...
>
> "Lloyd Dupont" <net.galador@ld> wrote in message
> news:ea8V0QUkDHA.2268@TK2MSFTNGP12.phx.gbl...
> > Does any of you have the slightest ideas of why you can't modify an
array
> > list while in a foreach of its element.
> >
> Because that is the rules when it comes to enumerators, it makes
enumerator
> implementation alot easier. Allowing changes requires a more
sophisticated,
> thread-safe enumerator. The probability of a bug in that case would be
very
> large in deed.
> > 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 ?
> >
> Well, simple method, stick a private field like int version; in your class
> and increment it on every change, following your proper locking rules and
> methodologies of course(you may need to use
> System.Threading.Interlocked.Increment in this case, assuming multiple
> threads, enumerators, and processors). Then on every command to the
> enumerator check that value, if it changed since the enumerator was
created,
> your enumerator is invalid and should throw an exception as specified in
the
> IEnumerable documentation.
>
>
> >
>
>


Grant Richins [MS]

10/13/2003 7:17:00 PM

0

Yes a lot of spurious locks would slow you down, but this is the perfect
place to use System.Threading.Interlocked.Increment(ref int).

--
--Grant
This posting is provided "AS IS" with no warranties, and confers no rights.


Daniel O'Connell [C# MVP]

10/13/2003 9:30:00 PM

0


"Lloyd Dupont" <net.galador@ld> wrote in message
news:OIy1u6XkDHA.2964@tk2msftngp13.phx.gbl...
> I like this int version inside a list, but if my list is gong to be used
in
> multithreaded environment, ain't all these lock() {} going to be a
> performance hit ?
>
Yes. I however advised using the proper locking because I didn't know what
you were doing precisely. If your class requires locking in other
situations(it needs to be thread safe, for example) then use locks, if not,
Interlocked.Increment(ref int) is quite sufficent for simply incrementing
the version field.
> "Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> a $BqD(Brit dans le
message
> de news:fTrib.550641$Oz4.497665@rwcrnsc54...
> >
> > "Lloyd Dupont" <net.galador@ld> wrote in message
> > news:ea8V0QUkDHA.2268@TK2MSFTNGP12.phx.gbl...
> > > Does any of you have the slightest ideas of why you can't modify an
> array
> > > list while in a foreach of its element.
> > >
> > Because that is the rules when it comes to enumerators, it makes
> enumerator
> > implementation alot easier. Allowing changes requires a more
> sophisticated,
> > thread-safe enumerator. The probability of a bug in that case would be
> very
> > large in deed.
> > > 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 ?
> > >
> > Well, simple method, stick a private field like int version; in your
class
> > and increment it on every change, following your proper locking rules
and
> > methodologies of course(you may need to use
> > System.Threading.Interlocked.Increment in this case, assuming multiple
> > threads, enumerators, and processors). Then on every command to the
> > enumerator check that value, if it changed since the enumerator was
> created,
> > your enumerator is invalid and should throw an exception as specified in
> the
> > IEnumerable documentation.
> >
> >
> > >
> >
> >
>
>


Lloyd Dupont

10/13/2003 11:15:00 PM

0

no worry I had an idea last night.
I lock in upper level user code, and the usage is preasumably safe.

on another though this Interlocked class, looks great, it works across
thread, but does it work across process ?
I was thinking to use a shared memory channel to share data but I've heard
that mutex was a performance hit and I had better design my own custom user
level "mutex"

"Grant Richins [MS]" <grantri@online.microsoft.com> wrote in message
news:uuufF6bkDHA.2416@TK2MSFTNGP10.phx.gbl...
> Yes a lot of spurious locks would slow you down, but this is the perfect
> place to use System.Threading.Interlocked.Increment(ref int).
>
> --
> --Grant
> This posting is provided "AS IS" with no warranties, and confers no
rights.
>
>


Lloyd Dupont

10/14/2003 11:10:00 PM

0

I don't understand what you said.
would you ind tell it otherwise ?

"Kalpesh Shah" <shahkalpesh@hotmail.com> wrote in message
news:%23HpzNBWkDHA.2312@TK2MSFTNGP12.phx.gbl...
> 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[i] = 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" <net.galador@ld> wrote in message
> news:ea8V0QUkDHA.2268@TK2MSFTNGP12.phx.gbl...
> > 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 ?
> >
> >
>
>
>
>