In the past, when the .NET Reflector was free, I used it to suffocate in the .NET Framework code. It seemed to me that most collections in .NET 2.0 (I believe that this is also applicable for current versions) use the following mechanism to recognize collection changes during loops:
public class SomeCollection<T>
{
internal int version = 0;
public void Add(T item)
{
version++;
}
public IEnumerator<T> GetEnumerator()
{
return new SomeCollectionEnumerator<T>(this);
}
}
public class SomeCollectionEnumerator<T> : IEnumerator<T>
{
private SomeCollection<T> collection;
private int version;
public SomeCollectionEnumerator(SomeCollection<T> collection)
{
this.version = collection.version;
this.collection = collection;
}
public bool MoveNext()
{
if (this.version != this.collection.version)
{
throw SomeException(...);
}
}
}
( -, ), ( .NET framework ) , . , int.MaxValue. , version++ ( , ).
, , unckecked version++. , .NET ? - , ?