From b2da1550ce685d17805fdaa0e1237e84e5689159 Mon Sep 17 00:00:00 2001 From: James Ko Date: Mon, 28 Nov 2016 17:06:25 -0500 Subject: [PATCH] Do not cache _items. Commit migrated from https://github.com/dotnet/coreclr/commit/9dd876f4c7cad071cee6c50f22b88650b7330b37 --- .../src/mscorlib/src/System/Collections/Generic/List.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/coreclr/src/mscorlib/src/System/Collections/Generic/List.cs b/src/coreclr/src/mscorlib/src/System/Collections/Generic/List.cs index dc885cf..940d721 100644 --- a/src/coreclr/src/mscorlib/src/System/Collections/Generic/List.cs +++ b/src/coreclr/src/mscorlib/src/System/Collections/Generic/List.cs @@ -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; } } } -- 2.7.4