Changing the built-in .NET collection int.MaxValue or more - overflow error

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;

    // code skipped for brevity

    public void Add(T item)
    {
        version++;

        // add item logic ...
    }

    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)
         {
             // collection was modified while iterated over
             throw SomeException(...);
         }
         // iteration logic here...
     }
}

( -, ), ( .NET framework ) , . , int.MaxValue. , version++ ( , ).

, , unckecked version++. , .NET ? - , ?

+5
1

, # . ( BCL.)

+4

All Articles