1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.Collections.ObjectModel;
6 using System.Diagnostics;
7 using System.Runtime.CompilerServices;
8 using System.Threading;
10 namespace System.Collections.Generic
12 // Implements a variable-size List that uses an array of objects to store the
13 // elements. A List has a capacity, which is the allocated length
14 // of the internal array. As elements are added to a List, the capacity
15 // of the List is automatically increased as required by reallocating the
18 [DebuggerTypeProxy(typeof(ICollectionDebugView<>))]
19 [DebuggerDisplay("Count = {Count}")]
21 [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
22 public class List<T> : IList<T>, IList, IReadOnlyList<T>
24 private const int DefaultCapacity = 4;
26 private T[] _items; // Do not rename (binary serialization)
27 private int _size; // Do not rename (binary serialization)
28 private int _version; // Do not rename (binary serialization)
30 private object _syncRoot;
32 private static readonly T[] s_emptyArray = new T[0];
34 // Constructs a List. The list is initially empty and has a capacity
35 // of zero. Upon adding the first element to the list the capacity is
36 // increased to DefaultCapacity, and then increased in multiples of two
40 _items = s_emptyArray;
43 // Constructs a List with a given initial capacity. The list is
44 // initially empty, but will have room for the given number of elements
45 // before any reallocations are required.
47 public List(int capacity)
50 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
53 _items = s_emptyArray;
55 _items = new T[capacity];
58 // Constructs a List, copying the contents of the given collection. The
59 // size and capacity of the new list will both be equal to the size of the
62 public List(IEnumerable<T> collection)
64 if (collection == null)
65 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
67 if (collection is ICollection<T> c)
72 _items = s_emptyArray;
76 _items = new T[count];
84 _items = s_emptyArray;
85 AddEnumerable(collection);
89 // Gets and sets the capacity of this list. The capacity is the size of
90 // the internal array used to hold items. When set, the internal
91 // array of the list is reallocated to the given capacity.
103 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
106 if (value != _items.Length)
110 T[] newItems = new T[value];
113 Array.Copy(_items, 0, newItems, 0, _size);
119 _items = s_emptyArray;
125 // Read-only property describing how many elements are in the List.
126 public int Count => _size;
128 bool IList.IsFixedSize => false;
130 // Is this List read-only?
131 bool ICollection<T>.IsReadOnly => false;
133 bool IList.IsReadOnly => false;
135 // Is this List synchronized (thread-safe)?
136 bool ICollection.IsSynchronized => false;
138 // Synchronization root for this object.
139 object ICollection.SyncRoot
143 if (_syncRoot == null)
145 Interlocked.CompareExchange<object>(ref _syncRoot, new object(), null);
151 // Sets or Gets the element at the given index.
152 public T this[int index]
156 // Following trick can reduce the range check by one
157 if ((uint)index >= (uint)_size)
159 ThrowHelper.ThrowArgumentOutOfRange_IndexException();
161 return _items[index];
167 if ((uint)index >= (uint)_size)
169 ThrowHelper.ThrowArgumentOutOfRange_IndexException();
171 _items[index] = value;
175 private static bool IsCompatibleObject(object value)
177 // Non-null values are fine. Only accept nulls if T is a class or Nullable<U>.
178 // Note that default(T) is not equal to null for value types except when T is Nullable<U>.
179 return ((value is T) || (value == null && default(T) == null));
182 object IList.this[int index]
190 ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value, ExceptionArgument.value);
194 this[index] = (T)value;
196 catch (InvalidCastException)
198 ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
203 // Adds the given object to the end of this list. The size of the list is
204 // increased by one. If required, the capacity of the list is doubled
205 // before adding the new element.
207 [MethodImpl(MethodImplOptions.AggressiveInlining)]
208 public void Add(T item)
213 if ((uint)size < (uint)array.Length)
224 // Non-inline from List.Add to improve its code quality as uncommon path
225 [MethodImpl(MethodImplOptions.NoInlining)]
226 private void AddWithResize(T item)
229 EnsureCapacity(size + 1);
234 int IList.Add(object item)
236 ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
242 catch (InvalidCastException)
244 ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
250 // Adds the elements of the given collection to the end of this list. If
251 // required, the capacity of the list is increased to twice the previous
252 // capacity or the new size, whichever is larger.
254 public void AddRange(IEnumerable<T> collection)
255 => InsertRange(_size, collection);
257 public ReadOnlyCollection<T> AsReadOnly()
258 => new ReadOnlyCollection<T>(this);
260 // Searches a section of the list for a given element using a binary search
261 // algorithm. Elements of the list are compared to the search value using
262 // the given IComparer interface. If comparer is null, elements of
263 // the list are compared to the search value using the IComparable
264 // interface, which in that case must be implemented by all elements of the
265 // list and the given search value. This method assumes that the given
266 // section of the list is already sorted; if this is not the case, the
267 // result will be incorrect.
269 // The method returns the index of the given value in the list. If the
270 // list does not contain the given value, the method returns a negative
271 // integer. The bitwise complement operator (~) can be applied to a
272 // negative result to produce the index of the first element (if any) that
273 // is larger than the given search value. This is also the index at which
274 // the search value should be inserted into the list in order for the list
277 // The method uses the Array.BinarySearch method to perform the
280 public int BinarySearch(int index, int count, T item, IComparer<T> comparer)
283 ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
285 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
286 if (_size - index < count)
287 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
289 return Array.BinarySearch<T>(_items, index, count, item, comparer);
292 public int BinarySearch(T item)
293 => BinarySearch(0, Count, item, null);
295 public int BinarySearch(T item, IComparer<T> comparer)
296 => BinarySearch(0, Count, item, comparer);
298 // Clears the contents of List.
299 [MethodImpl(MethodImplOptions.AggressiveInlining)]
303 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
309 Array.Clear(_items, 0, size); // Clear the elements so that the gc can reclaim the references.
318 // Contains returns true if the specified element is in the List.
319 // It does a linear, O(n) search. Equality is determined by calling
320 // EqualityComparer<T>.Default.Equals().
322 public bool Contains(T item)
324 // PERF: IndexOf calls Array.IndexOf, which internally
325 // calls EqualityComparer<T>.Default.IndexOf, which
326 // is specialized for different types. This
327 // boosts performance since instead of making a
328 // virtual method call each iteration of the loop,
329 // via EqualityComparer<T>.Default.Equals, we
330 // only make one virtual call to EqualityComparer.IndexOf.
332 return _size != 0 && IndexOf(item) != -1;
335 bool IList.Contains(object item)
337 if (IsCompatibleObject(item))
339 return Contains((T)item);
344 public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
346 if (converter == null)
348 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);
351 List<TOutput> list = new List<TOutput>(_size);
352 for (int i = 0; i < _size; i++)
354 list._items[i] = converter(_items[i]);
360 // Copies this List into array, which must be of a
361 // compatible array type.
362 public void CopyTo(T[] array)
365 // Copies this List into array, which must be of a
366 // compatible array type.
367 void ICollection.CopyTo(Array array, int arrayIndex)
369 if ((array != null) && (array.Rank != 1))
371 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
376 // Array.Copy will check for NULL.
377 Array.Copy(_items, 0, array, arrayIndex, _size);
379 catch (ArrayTypeMismatchException)
381 ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
385 // Copies a section of this list to the given array at the given index.
387 // The method uses the Array.Copy method to copy the elements.
389 public void CopyTo(int index, T[] array, int arrayIndex, int count)
391 if (_size - index < count)
393 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
396 // Delegate rest of error checking to Array.Copy.
397 Array.Copy(_items, index, array, arrayIndex, count);
400 public void CopyTo(T[] array, int arrayIndex)
402 // Delegate rest of error checking to Array.Copy.
403 Array.Copy(_items, 0, array, arrayIndex, _size);
406 // Ensures that the capacity of this list is at least the given minimum
407 // value. If the current capacity of the list is less than min, the
408 // capacity is increased to twice the current capacity or to min,
409 // whichever is larger.
411 private void EnsureCapacity(int min)
413 if (_items.Length < min)
415 int newCapacity = _items.Length == 0 ? DefaultCapacity : _items.Length * 2;
416 // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
417 // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
418 if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
419 if (newCapacity < min) newCapacity = min;
420 Capacity = newCapacity;
424 public bool Exists(Predicate<T> match)
425 => FindIndex(match) != -1;
427 public T Find(Predicate<T> match)
431 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
434 for (int i = 0; i < _size; i++)
436 if (match(_items[i]))
444 public List<T> FindAll(Predicate<T> match)
448 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
451 List<T> list = new List<T>();
452 for (int i = 0; i < _size; i++)
454 if (match(_items[i]))
462 public int FindIndex(Predicate<T> match)
463 => FindIndex(0, _size, match);
465 public int FindIndex(int startIndex, Predicate<T> match)
466 => FindIndex(startIndex, _size - startIndex, match);
468 public int FindIndex(int startIndex, int count, Predicate<T> match)
470 if ((uint)startIndex > (uint)_size)
472 ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
475 if (count < 0 || startIndex > _size - count)
477 ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
482 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
485 int endIndex = startIndex + count;
486 for (int i = startIndex; i < endIndex; i++)
488 if (match(_items[i])) return i;
493 public T FindLast(Predicate<T> match)
497 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
500 for (int i = _size - 1; i >= 0; i--)
502 if (match(_items[i]))
510 public int FindLastIndex(Predicate<T> match)
511 => FindLastIndex(_size - 1, _size, match);
513 public int FindLastIndex(int startIndex, Predicate<T> match)
514 => FindLastIndex(startIndex, startIndex + 1, match);
516 public int FindLastIndex(int startIndex, int count, Predicate<T> match)
520 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
525 // Special case for 0 length List
526 if (startIndex != -1)
528 ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
533 // Make sure we're not out of range
534 if ((uint)startIndex >= (uint)_size)
536 ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
540 // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0.
541 if (count < 0 || startIndex - count + 1 < 0)
543 ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
546 int endIndex = startIndex - count;
547 for (int i = startIndex; i > endIndex; i--)
549 if (match(_items[i]))
557 public void ForEach(Action<T> action)
561 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action);
564 int version = _version;
566 for (int i = 0; i < _size; i++)
568 if (version != _version)
575 if (version != _version)
576 ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
579 // Returns an enumerator for this list with the given
580 // permission for removal of elements. If modifications made to the list
581 // while an enumeration is in progress, the MoveNext and
582 // GetObject methods of the enumerator will throw an exception.
584 public Enumerator GetEnumerator()
585 => new Enumerator(this);
587 IEnumerator<T> IEnumerable<T>.GetEnumerator()
588 => new Enumerator(this);
590 IEnumerator IEnumerable.GetEnumerator()
591 => new Enumerator(this);
593 public List<T> GetRange(int index, int count)
597 ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
602 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
605 if (_size - index < count)
607 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
610 List<T> list = new List<T>(count);
611 Array.Copy(_items, index, list._items, 0, count);
617 // Returns the index of the first occurrence of a given value in a range of
618 // this list. The list is searched forwards from beginning to end.
619 // The elements of the list are compared to the given value using the
620 // Object.Equals method.
622 // This method uses the Array.IndexOf method to perform the
625 public int IndexOf(T item)
626 => Array.IndexOf(_items, item, 0, _size);
628 int IList.IndexOf(object item)
630 if (IsCompatibleObject(item))
632 return IndexOf((T)item);
637 // Returns the index of the first occurrence of a given value in a range of
638 // this list. The list is searched forwards, starting at index
639 // index and ending at count number of elements. The
640 // elements of the list are compared to the given value using the
641 // Object.Equals method.
643 // This method uses the Array.IndexOf method to perform the
646 public int IndexOf(T item, int index)
649 ThrowHelper.ThrowArgumentOutOfRange_IndexException();
650 return Array.IndexOf(_items, item, index, _size - index);
653 // Returns the index of the first occurrence of a given value in a range of
654 // this list. The list is searched forwards, starting at index
655 // index and upto count number of elements. The
656 // elements of the list are compared to the given value using the
657 // Object.Equals method.
659 // This method uses the Array.IndexOf method to perform the
662 public int IndexOf(T item, int index, int count)
665 ThrowHelper.ThrowArgumentOutOfRange_IndexException();
667 if (count < 0 || index > _size - count)
668 ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
670 return Array.IndexOf(_items, item, index, count);
673 // Inserts an element into this list at a given index. The size of the list
674 // is increased by one. If required, the capacity of the list is doubled
675 // before inserting the new element.
677 public void Insert(int index, T item)
679 // Note that insertions at the end are legal.
680 if ((uint)index > (uint)_size)
682 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
684 if (_size == _items.Length) EnsureCapacity(_size + 1);
687 Array.Copy(_items, index, _items, index + 1, _size - index);
689 _items[index] = item;
694 void IList.Insert(int index, object item)
696 ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
700 Insert(index, (T)item);
702 catch (InvalidCastException)
704 ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
708 // Inserts the elements of the given collection at a given index. If
709 // required, the capacity of the list is increased to twice the previous
710 // capacity or the new size, whichever is larger. Ranges may be added
711 // to the end of the list by setting index to the List's size.
713 public void InsertRange(int index, IEnumerable<T> collection)
715 if (collection == null)
717 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
720 if ((uint)index > (uint)_size)
722 ThrowHelper.ThrowArgumentOutOfRange_IndexException();
725 if (collection is ICollection<T> c)
730 EnsureCapacity(_size + count);
733 Array.Copy(_items, index, _items, index + count, _size - index);
736 // If we're inserting a List into itself, we want to be able to deal with that.
739 // Copy first part of _items to insert location
740 Array.Copy(_items, 0, _items, index, index);
741 // Copy last part of _items back to inserted location
742 Array.Copy(_items, index + count, _items, index * 2, _size - index);
746 c.CopyTo(_items, index);
751 else if (index < _size)
753 // We're inserting a lazy enumerable. Call Insert on each of the constituent items.
754 using (IEnumerator<T> en = collection.GetEnumerator())
756 while (en.MoveNext())
758 Insert(index++, en.Current);
764 // We're adding a lazy enumerable because the index is at the end of this list.
765 AddEnumerable(collection);
770 // Returns the index of the last occurrence of a given value in a range of
771 // this list. The list is searched backwards, starting at the end
772 // and ending at the first element in the list. The elements of the list
773 // are compared to the given value using the Object.Equals method.
775 // This method uses the Array.LastIndexOf method to perform the
778 public int LastIndexOf(T item)
781 { // Special case for empty list
786 return LastIndexOf(item, _size - 1, _size);
790 // Returns the index of the last occurrence of a given value in a range of
791 // this list. The list is searched backwards, starting at index
792 // index and ending at the first element in the list. The
793 // elements of the list are compared to the given value using the
794 // Object.Equals method.
796 // This method uses the Array.LastIndexOf method to perform the
799 public int LastIndexOf(T item, int index)
802 ThrowHelper.ThrowArgumentOutOfRange_IndexException();
803 return LastIndexOf(item, index, index + 1);
806 // Returns the index of the last occurrence of a given value in a range of
807 // this list. The list is searched backwards, starting at index
808 // index and upto count elements. The elements of
809 // the list are compared to the given value using the Object.Equals
812 // This method uses the Array.LastIndexOf method to perform the
815 public int LastIndexOf(T item, int index, int count)
817 if ((Count != 0) && (index < 0))
819 ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
822 if ((Count != 0) && (count < 0))
824 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
828 { // Special case for empty list
834 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
837 if (count > index + 1)
839 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
842 return Array.LastIndexOf(_items, item, index, count);
845 // Removes the element at the given index. The size of the list is
847 public bool Remove(T item)
849 int index = IndexOf(item);
859 void IList.Remove(object item)
861 if (IsCompatibleObject(item))
867 // This method removes all items which matches the predicate.
868 // The complexity is O(n).
869 public int RemoveAll(Predicate<T> match)
873 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
876 int freeIndex = 0; // the first free slot in items array
878 // Find the first item which needs to be removed.
879 while (freeIndex < _size && !match(_items[freeIndex])) freeIndex++;
880 if (freeIndex >= _size) return 0;
882 int current = freeIndex + 1;
883 while (current < _size)
885 // Find the first item which needs to be kept.
886 while (current < _size && match(_items[current])) current++;
890 // copy item to the free slot.
891 _items[freeIndex++] = _items[current++];
895 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
897 Array.Clear(_items, freeIndex, _size - freeIndex); // Clear the elements so that the gc can reclaim the references.
900 int result = _size - freeIndex;
906 // Removes the element at the given index. The size of the list is
908 public void RemoveAt(int index)
910 if ((uint)index >= (uint)_size)
912 ThrowHelper.ThrowArgumentOutOfRange_IndexException();
917 Array.Copy(_items, index + 1, _items, index, _size - index);
919 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
921 _items[_size] = default;
926 // Removes a range of elements from this list.
927 public void RemoveRange(int index, int count)
931 ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
936 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
939 if (_size - index < count)
940 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
948 Array.Copy(_items, index + count, _items, index, _size - index);
952 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
954 Array.Clear(_items, _size, count);
959 // Reverses the elements in this list.
960 public void Reverse()
961 => Reverse(0, Count);
963 // Reverses the elements in a range of this list. Following a call to this
964 // method, an element in the range given by index and count
965 // which was previously located at index i will now be located at
966 // index index + (index + count - i - 1).
968 public void Reverse(int index, int count)
972 ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
977 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
980 if (_size - index < count)
981 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
985 Array.Reverse(_items, index, count);
990 // Sorts the elements in this list. Uses the default comparer and
993 => Sort(0, Count, null);
995 // Sorts the elements in this list. Uses Array.Sort with the
996 // provided comparer.
997 public void Sort(IComparer<T> comparer)
998 => Sort(0, Count, comparer);
1000 // Sorts the elements in a section of this list. The sort compares the
1001 // elements to each other using the given IComparer interface. If
1002 // comparer is null, the elements are compared to each other using
1003 // the IComparable interface, which in that case must be implemented by all
1004 // elements of the list.
1006 // This method uses the Array.Sort method to sort the elements.
1008 public void Sort(int index, int count, IComparer<T> comparer)
1012 ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
1017 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1020 if (_size - index < count)
1021 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
1025 Array.Sort<T>(_items, index, count, comparer);
1030 public void Sort(Comparison<T> comparison)
1032 if (comparison == null)
1034 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparison);
1039 ArraySortHelper<T>.Sort(_items, 0, _size, comparison);
1044 // ToArray returns an array containing the contents of the List.
1045 // This requires copying the List, which is an O(n) operation.
1046 public T[] ToArray()
1050 return s_emptyArray;
1053 T[] array = new T[_size];
1054 Array.Copy(_items, 0, array, 0, _size);
1058 // Sets the capacity of this list to the size of the list. This method can
1059 // be used to minimize a list's memory overhead once it is known that no
1060 // new elements will be added to the list. To completely clear a list and
1061 // release all memory referenced by the list, execute the following
1065 // list.TrimExcess();
1067 public void TrimExcess()
1069 int threshold = (int)(((double)_items.Length) * 0.9);
1070 if (_size < threshold)
1076 public bool TrueForAll(Predicate<T> match)
1080 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
1083 for (int i = 0; i < _size; i++)
1085 if (!match(_items[i]))
1093 private void AddEnumerable(IEnumerable<T> enumerable)
1095 Debug.Assert(enumerable != null);
1096 Debug.Assert(!(enumerable is ICollection<T>), "We should have optimized for this beforehand.");
1098 _version++; // Even if the enumerable has no items, we can update _version.
1099 using (IEnumerator<T> en = enumerable.GetEnumerator())
1102 while (en.MoveNext())
1104 // Capture Current before doing anything else. If this throws
1105 // an exception, we want to make a clean break.
1106 T current = en.Current;
1108 if (_size == _items.Length)
1110 EnsureCapacity(_size + 1);
1113 _items[_size++] = current;
1118 public struct Enumerator : IEnumerator<T>, IEnumerator
1120 private List<T> _list;
1122 private int _version;
1125 internal Enumerator(List<T> list)
1129 _version = list._version;
1133 public void Dispose()
1137 public bool MoveNext()
1139 List<T> localList = _list;
1141 if (_version == localList._version && ((uint)_index < (uint)localList._size))
1143 _current = localList._items[_index];
1147 return MoveNextRare();
1150 private bool MoveNextRare()
1152 if (_version != _list._version)
1154 ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
1157 _index = _list._size + 1;
1162 public T Current => _current;
1164 object IEnumerator.Current
1168 if (_index == 0 || _index == _list._size + 1)
1170 ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen();
1176 void IEnumerator.Reset()
1178 if (_version != _list._version)
1180 ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();