Skip to content

Commit 8b08265

Browse files
authored
Remove List<T>.Enumerator.MoveNextRare (#118425)
1 parent 6b4936e commit 8b08265

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

src/libraries/Common/tests/System/Collections/IEnumerableTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public abstract class IEnumerableTest<T>
2020
protected T DefaultValue => default(T);
2121

2222
protected bool MoveNextAtEndThrowsOnModifiedCollection => true;
23+
protected virtual bool CurrentAfterFullEnumerationThrows => true;
2324

2425
protected virtual CollectionOrder CollectionOrder => CollectionOrder.Sequential;
2526

@@ -352,7 +353,7 @@ public void EnumeratePastEndThenModify()
352353
VerifyModifiedEnumerator(
353354
enumerator,
354355
DefaultValue,
355-
false,
356+
CurrentAfterFullEnumerationThrows,
356357
true);
357358
}
358359

src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ public ReadOnlyDictionaryOverNonGenericTests()
391391
protected override bool IsGenericCompatibility { get { return false; } }
392392
protected override bool ItemsMustBeUnique { get { return true; } }
393393
protected override bool ItemsMustBeNonNull { get { return true; } }
394+
protected override bool CurrentAfterFullEnumerationThrows { get { return false; } }
394395
protected override object GenerateItem()
395396
{
396397
return new KeyValuePair<string, int>(m_next_item.ToString(), m_next_item++);
@@ -429,6 +430,7 @@ public ReadOnlyDictionaryTestsStringInt()
429430
protected override bool IsGenericCompatibility { get { return false; } }
430431
protected override bool ItemsMustBeUnique { get { return true; } }
431432
protected override bool ItemsMustBeNonNull { get { return true; } }
433+
protected override bool CurrentAfterFullEnumerationThrows { get { return false; } }
432434
protected override object GenerateItem()
433435
{
434436
return new KeyValuePair<string, int>(m_next_item.ToString(), m_next_item++);

src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,16 +1185,15 @@ public bool TrueForAll(Predicate<T> match)
11851185
public struct Enumerator : IEnumerator<T>, IEnumerator
11861186
{
11871187
private readonly List<T> _list;
1188-
private int _index;
11891188
private readonly int _version;
1189+
1190+
private int _index;
11901191
private T? _current;
11911192

11921193
internal Enumerator(List<T> list)
11931194
{
11941195
_list = list;
1195-
_index = 0;
11961196
_version = list._version;
1197-
_current = default;
11981197
}
11991198

12001199
public void Dispose()
@@ -1205,24 +1204,20 @@ public bool MoveNext()
12051204
{
12061205
List<T> localList = _list;
12071206

1208-
if (_version == localList._version && ((uint)_index < (uint)localList._size))
1207+
if (_version != _list._version)
12091208
{
1210-
_current = localList._items[_index];
1211-
_index++;
1212-
return true;
1209+
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
12131210
}
1214-
return MoveNextRare();
1215-
}
12161211

1217-
private bool MoveNextRare()
1218-
{
1219-
if (_version != _list._version)
1212+
if ((uint)_index < (uint)localList._size)
12201213
{
1221-
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
1214+
_current = localList._items[_index];
1215+
_index++;
1216+
return true;
12221217
}
12231218

1224-
_index = _list._size + 1;
12251219
_current = default;
1220+
_index = -1;
12261221
return false;
12271222
}
12281223

@@ -1232,11 +1227,12 @@ private bool MoveNextRare()
12321227
{
12331228
get
12341229
{
1235-
if (_index == 0 || _index == _list._size + 1)
1230+
if (_index <= 0)
12361231
{
12371232
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen();
12381233
}
1239-
return Current;
1234+
1235+
return _current;
12401236
}
12411237
}
12421238

0 commit comments

Comments
 (0)