5

Back in time when .NET Reflector was free I used it to gasp into the .NET framework code. It came to my attention that most collections in .NET 2.0 (I believe this is applicable for current versions too) use the following mechanism to recognize collection modifications 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...
     }
}

Now imagine the hypothetical case of a long running application (a heavily used web service that must have minimal downtime and should be stable and reliable) that keeps a given collection instance (one of the built-in collection types in .NET framework) in memory for as long as it runs. The collection gets modified frequently enough so that int.MaxValue modifications are possible to happen. Is there a risk that the version++ line in every modification method of the collection throws an overflow exception (assuming the overflow checks are not globally disabled).

I must admit that I have weak memories of the details of the reflected code, but I do not remember usage of unckecked blocks around version++ operations. Does this mean built-in collection types in .NET are not suitable for the purpose of such long-time running application scenario? Just out of curiosity, has someone encountered a real-life scenario where this could actually happen?

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
  • 1
    dotPeek ( http://www.jetbrains.com/decompiler/ ) is a free and good decompiler, in case the Reflector story left a sour taste in your mouth. – spender Aug 11 '12 at 22:10
  • 1
    Also the BCL source code (and most other MSFT .NET libraries) is shared and available at http://referencesource.microsoft.com/netframework.aspx – Michael Graczyk Aug 11 '12 at 22:12
  • 1
    @spender thank you, to me the feeling is more like being blinded rather than having a sour taste, but I guess we both mean the same thing. Another useful thing to learn today - a new decompiler tool, even from jetbrains – Ivaylo Slavov Aug 11 '12 at 22:14
  • 2
    ILSpy is still free, and since it's open source, should it ever get a $35 price-tag and a time-bomb in the last free update, you could just fork it. – Jon Hanna Aug 11 '12 at 23:23

1 Answers1

4

No, because the c# compiler makes integer arithmetic unchecked by default. (This includes the compiled BCL.)

Michael Graczyk
  • 4,905
  • 2
  • 22
  • 34
  • Nice, I did not know that. I guess I could then safely venture into writing an application that would make `int.MaxValue` collection modifications one day :) Thanks for the speedy reply. – Ivaylo Slavov Aug 11 '12 at 22:11