Do not cache _items.
authorJames Ko <jamesqko@gmail.com>
Mon, 28 Nov 2016 22:06:25 +0000 (17:06 -0500)
committerJames Ko <jamesqko@gmail.com>
Mon, 28 Nov 2016 22:06:25 +0000 (17:06 -0500)
Commit migrated from https://github.com/dotnet/coreclr/commit/9dd876f4c7cad071cee6c50f22b88650b7330b37

src/coreclr/src/mscorlib/src/System/Collections/Generic/List.cs

index dc885cf..940d721 100644 (file)
@@ -386,7 +386,7 @@ namespace System.Collections.Generic {
         }
 
         // Ensures that the capacity of this list is at least the given minimum
-        // value. If the currect capacity of the list is less than min, the
+        // value. If the current capacity of the list is less than min, the
         // capacity is increased to twice the current capacity or to min,
         // whichever is larger.
         private void EnsureCapacity(int min) {
@@ -1037,20 +1037,18 @@ namespace System.Collections.Generic {
             {
                 _version++; // Even if the enumerable has no items, we can update _version.
 
-                T[] items = _items;
-
                 while (en.MoveNext())
                 {
-                    if (_size == items.Length)
+                    // Capture Current before doing anything else. If this throws
+                    // an exception, we want to make a clean break.
+                    T current = en.Current;
+
+                    if (_size == _items.Length)
                     {
                         EnsureCapacity(_size + 1);
-                        items = _items;
                     }
 
-                    // Note: It's important we increment size after Current is called.
-                    // If that throws an exception we don't want to do the increment.
-                    items[_size] = en.Current;
-                    _size++;
+                    _items[_size++] = current;
                 }
             }
         }