diff --git a/src/libraries/Common/tests/System/Collections/IEnumerableTest.cs b/src/libraries/Common/tests/System/Collections/IEnumerableTest.cs index a52bae97e4eceb..ccf2a1d5c69d9f 100644 --- a/src/libraries/Common/tests/System/Collections/IEnumerableTest.cs +++ b/src/libraries/Common/tests/System/Collections/IEnumerableTest.cs @@ -20,6 +20,7 @@ public abstract class IEnumerableTest protected T DefaultValue => default(T); protected bool MoveNextAtEndThrowsOnModifiedCollection => true; + protected virtual bool CurrentAfterFullEnumerationThrows => true; protected virtual CollectionOrder CollectionOrder => CollectionOrder.Sequential; @@ -352,7 +353,7 @@ public void EnumeratePastEndThenModify() VerifyModifiedEnumerator( enumerator, DefaultValue, - false, + CurrentAfterFullEnumerationThrows, true); } diff --git a/src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs b/src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs index fc22f2c68cbe2f..31d247a4dd15df 100644 --- a/src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs +++ b/src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs @@ -391,6 +391,7 @@ public ReadOnlyDictionaryOverNonGenericTests() protected override bool IsGenericCompatibility { get { return false; } } protected override bool ItemsMustBeUnique { get { return true; } } protected override bool ItemsMustBeNonNull { get { return true; } } + protected override bool CurrentAfterFullEnumerationThrows { get { return false; } } protected override object GenerateItem() { return new KeyValuePair(m_next_item.ToString(), m_next_item++); @@ -429,6 +430,7 @@ public ReadOnlyDictionaryTestsStringInt() protected override bool IsGenericCompatibility { get { return false; } } protected override bool ItemsMustBeUnique { get { return true; } } protected override bool ItemsMustBeNonNull { get { return true; } } + protected override bool CurrentAfterFullEnumerationThrows { get { return false; } } protected override object GenerateItem() { return new KeyValuePair(m_next_item.ToString(), m_next_item++); diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs index 1df1ea89696c5f..d9af589f8f268d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs @@ -1185,16 +1185,15 @@ public bool TrueForAll(Predicate match) public struct Enumerator : IEnumerator, IEnumerator { private readonly List _list; - private int _index; private readonly int _version; + + private int _index; private T? _current; internal Enumerator(List list) { _list = list; - _index = 0; _version = list._version; - _current = default; } public void Dispose() @@ -1205,24 +1204,20 @@ public bool MoveNext() { List localList = _list; - if (_version == localList._version && ((uint)_index < (uint)localList._size)) + if (_version != _list._version) { - _current = localList._items[_index]; - _index++; - return true; + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } - return MoveNextRare(); - } - private bool MoveNextRare() - { - if (_version != _list._version) + if ((uint)_index < (uint)localList._size) { - ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); + _current = localList._items[_index]; + _index++; + return true; } - _index = _list._size + 1; _current = default; + _index = -1; return false; } @@ -1232,11 +1227,12 @@ private bool MoveNextRare() { get { - if (_index == 0 || _index == _list._size + 1) + if (_index <= 0) { ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen(); } - return Current; + + return _current; } }