D
Daniel O'Connell
Sasha said:Hi,
I am extending standard IEnumerator, and I was just wondering what is the
best way to make enumarator safe? What do I mean by safe? Detect deletes and
all...
My idea is to have private Guid state field in the collection, and every
time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid received in
the begging to the current one. If they are different, the collection has
changed.
That would work, but would probably be overkill. It would be simple enough
just to maintain an Int32 or Int64 version counter and increment it every
time the collection changes, store the value when the IEnumerator is created
and check against it. It would probably be faster and less troublesome, just
a version++; \ version = version+1 to update(depending on your language, not
sure if vb.net has an increment operator...). Its not likely your collection
is going to change enough to overflow an Int32, let alone an Int64.