mscorlib(4.0.0.0) API with additions
List.cs
2 using System.Diagnostics;
4 using System.Threading;
5 
7 {
10  [Serializable]
11  [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
12  [DebuggerDisplay("Count = {Count}")]
13  [__DynamicallyInvokable]
15  {
16  [Serializable]
17  internal class SynchronizedList : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable
18  {
19  private List<T> _list;
20 
21  private object _root;
22 
23  public int Count
24  {
25  get
26  {
27  lock (_root)
28  {
29  return _list.Count;
30  }
31  }
32  }
33 
34  public bool IsReadOnly => ((ICollection<T>)_list).IsReadOnly;
35 
36  public T this[int index]
37  {
38  get
39  {
40  lock (_root)
41  {
42  return _list[index];
43  }
44  }
45  set
46  {
47  lock (_root)
48  {
49  _list[index] = value;
50  }
51  }
52  }
53 
54  internal SynchronizedList(List<T> list)
55  {
56  _list = list;
57  _root = ((ICollection)list).SyncRoot;
58  }
59 
60  public void Add(T item)
61  {
62  lock (_root)
63  {
64  _list.Add(item);
65  }
66  }
67 
68  public void Clear()
69  {
70  lock (_root)
71  {
72  _list.Clear();
73  }
74  }
75 
76  public bool Contains(T item)
77  {
78  lock (_root)
79  {
80  return _list.Contains(item);
81  }
82  }
83 
84  public void CopyTo(T[] array, int arrayIndex)
85  {
86  lock (_root)
87  {
88  _list.CopyTo(array, arrayIndex);
89  }
90  }
91 
92  public bool Remove(T item)
93  {
94  lock (_root)
95  {
96  return _list.Remove(item);
97  }
98  }
99 
101  {
102  lock (_root)
103  {
104  return _list.GetEnumerator();
105  }
106  }
107 
109  {
110  lock (_root)
111  {
112  return ((IEnumerable<T>)_list).GetEnumerator();
113  }
114  }
115 
116  public int IndexOf(T item)
117  {
118  lock (_root)
119  {
120  return _list.IndexOf(item);
121  }
122  }
123 
124  public void Insert(int index, T item)
125  {
126  lock (_root)
127  {
128  _list.Insert(index, item);
129  }
130  }
131 
132  public void RemoveAt(int index)
133  {
134  lock (_root)
135  {
136  _list.RemoveAt(index);
137  }
138  }
139  }
140 
142  [Serializable]
143  [__DynamicallyInvokable]
145  {
146  private List<T> list;
147 
148  private int index;
149 
150  private int version;
151 
152  private T current;
153 
156  [__DynamicallyInvokable]
157  public T Current
158  {
159  [__DynamicallyInvokable]
160  get
161  {
162  return current;
163  }
164  }
165 
169  [__DynamicallyInvokable]
170  object IEnumerator.Current
171  {
172  [__DynamicallyInvokable]
173  get
174  {
175  if (index == 0 || index == list._size + 1)
176  {
177  ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
178  }
179  return Current;
180  }
181  }
182 
183  internal Enumerator(List<T> list)
184  {
185  this.list = list;
186  index = 0;
187  version = list._version;
188  current = default(T);
189  }
190 
192  [__DynamicallyInvokable]
193  public void Dispose()
194  {
195  }
196 
201  [__DynamicallyInvokable]
202  public bool MoveNext()
203  {
204  List<T> list = this.list;
205  if (version == list._version && (uint)index < (uint)list._size)
206  {
207  current = list._items[index];
208  index++;
209  return true;
210  }
211  return MoveNextRare();
212  }
213 
214  private bool MoveNextRare()
215  {
216  if (version != list._version)
217  {
218  ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
219  }
220  index = list._size + 1;
221  current = default(T);
222  return false;
223  }
224 
227  [__DynamicallyInvokable]
229  {
230  if (version != list._version)
231  {
232  ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
233  }
234  index = 0;
235  current = default(T);
236  }
237  }
238 
239  private const int _defaultCapacity = 4;
240 
241  private T[] _items;
242 
243  private int _size;
244 
245  private int _version;
246 
247  [NonSerialized]
248  private object _syncRoot;
249 
250  private static readonly T[] _emptyArray = new T[0];
251 
257  [__DynamicallyInvokable]
258  public int Capacity
259  {
260  [__DynamicallyInvokable]
261  get
262  {
263  return _items.Length;
264  }
265  [__DynamicallyInvokable]
266  set
267  {
268  if (value < _size)
269  {
270  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
271  }
272  if (value == _items.Length)
273  {
274  return;
275  }
276  if (value > 0)
277  {
278  T[] array = new T[value];
279  if (_size > 0)
280  {
281  Array.Copy(_items, 0, array, 0, _size);
282  }
283  _items = array;
284  }
285  else
286  {
287  _items = _emptyArray;
288  }
289  }
290  }
291 
294  [__DynamicallyInvokable]
295  public int Count
296  {
297  [__DynamicallyInvokable]
298  get
299  {
300  return _size;
301  }
302  }
303 
307  [__DynamicallyInvokable]
308  bool IList.IsFixedSize
309  {
310  [__DynamicallyInvokable]
311  get
312  {
313  return false;
314  }
315  }
316 
320  [__DynamicallyInvokable]
321  bool ICollection<T>.IsReadOnly
322  {
323  [__DynamicallyInvokable]
324  get
325  {
326  return false;
327  }
328  }
329 
333  [__DynamicallyInvokable]
334  bool IList.IsReadOnly
335  {
336  [__DynamicallyInvokable]
337  get
338  {
339  return false;
340  }
341  }
342 
346  [__DynamicallyInvokable]
347  bool ICollection.IsSynchronized
348  {
349  [__DynamicallyInvokable]
350  get
351  {
352  return false;
353  }
354  }
355 
358  [__DynamicallyInvokable]
359  object ICollection.SyncRoot
360  {
361  [__DynamicallyInvokable]
362  get
363  {
364  if (_syncRoot == null)
365  {
366  Interlocked.CompareExchange<object>(ref _syncRoot, new object(), (object)null);
367  }
368  return _syncRoot;
369  }
370  }
371 
378  [__DynamicallyInvokable]
379  public T this[int index]
380  {
381  [__DynamicallyInvokable]
382  get
383  {
384  if ((uint)index >= (uint)_size)
385  {
386  ThrowHelper.ThrowArgumentOutOfRangeException();
387  }
388  return _items[index];
389  }
390  [__DynamicallyInvokable]
391  set
392  {
393  if ((uint)index >= (uint)_size)
394  {
395  ThrowHelper.ThrowArgumentOutOfRangeException();
396  }
397  _items[index] = value;
398  _version++;
399  }
400  }
401 
408  [__DynamicallyInvokable]
409  object IList.this[int index]
410  {
411  [__DynamicallyInvokable]
412  get
413  {
414  return this[index];
415  }
416  [__DynamicallyInvokable]
417  set
418  {
419  ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value, ExceptionArgument.value);
420  try
421  {
422  this[index] = (T)value;
423  }
424  catch (InvalidCastException)
425  {
426  ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
427  }
428  }
429  }
430 
432  [__DynamicallyInvokable]
433  public List()
434  {
435  _items = _emptyArray;
436  }
437 
442  [__DynamicallyInvokable]
443  public List(int capacity)
444  {
445  if (capacity < 0)
446  {
447  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
448  }
449  if (capacity == 0)
450  {
451  _items = _emptyArray;
452  }
453  else
454  {
455  _items = new T[capacity];
456  }
457  }
458 
463  [__DynamicallyInvokable]
464  public List(IEnumerable<T> collection)
465  {
466  if (collection == null)
467  {
468  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
469  }
470  ICollection<T> collection2 = collection as ICollection<T>;
471  if (collection2 != null)
472  {
473  int count = collection2.Count;
474  if (count == 0)
475  {
476  _items = _emptyArray;
477  return;
478  }
479  _items = new T[count];
480  collection2.CopyTo(_items, 0);
481  _size = count;
482  }
483  else
484  {
485  _size = 0;
486  _items = _emptyArray;
487  foreach (T item in collection)
488  {
489  Add(item);
490  }
491  }
492  }
493 
494  private static bool IsCompatibleObject(object value)
495  {
496  if (!(value is T))
497  {
498  if (value == null)
499  {
500  return default(T) == null;
501  }
502  return false;
503  }
504  return true;
505  }
506 
509  [__DynamicallyInvokable]
510  public void Add(T item)
511  {
512  if (_size == _items.Length)
513  {
514  EnsureCapacity(_size + 1);
515  }
516  _items[_size++] = item;
517  _version++;
518  }
519 
525  [__DynamicallyInvokable]
526  int IList.Add(object item)
527  {
528  ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
529  try
530  {
531  Add((T)item);
532  }
533  catch (InvalidCastException)
534  {
535  ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
536  }
537  return Count - 1;
538  }
539 
544  [__DynamicallyInvokable]
545  public void AddRange(IEnumerable<T> collection)
546  {
547  InsertRange(_size, collection);
548  }
549 
552  [__DynamicallyInvokable]
554  {
555  return new ReadOnlyCollection<T>(this);
556  }
557 
571  [__DynamicallyInvokable]
572  public int BinarySearch(int index, int count, T item, IComparer<T> comparer)
573  {
574  if (index < 0)
575  {
576  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
577  }
578  if (count < 0)
579  {
580  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
581  }
582  if (_size - index < count)
583  {
584  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
585  }
586  return Array.BinarySearch(_items, index, count, item, comparer);
587  }
588 
593  [__DynamicallyInvokable]
594  public int BinarySearch(T item)
595  {
596  return BinarySearch(0, Count, item, null);
597  }
598 
606  [__DynamicallyInvokable]
607  public int BinarySearch(T item, IComparer<T> comparer)
608  {
609  return BinarySearch(0, Count, item, comparer);
610  }
611 
613  [__DynamicallyInvokable]
614  public void Clear()
615  {
616  if (_size > 0)
617  {
618  Array.Clear(_items, 0, _size);
619  _size = 0;
620  }
621  _version++;
622  }
623 
628  [__DynamicallyInvokable]
629  public bool Contains(T item)
630  {
631  if (item == null)
632  {
633  for (int i = 0; i < _size; i++)
634  {
635  if (_items[i] == null)
636  {
637  return true;
638  }
639  }
640  return false;
641  }
643  for (int j = 0; j < _size; j++)
644  {
645  if (@default.Equals(_items[j], item))
646  {
647  return true;
648  }
649  }
650  return false;
651  }
652 
657  [__DynamicallyInvokable]
658  bool IList.Contains(object item)
659  {
660  if (IsCompatibleObject(item))
661  {
662  return Contains((T)item);
663  }
664  return false;
665  }
666 
673  public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
674  {
675  if (converter == null)
676  {
677  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);
678  }
679  List<TOutput> list = new List<TOutput>(_size);
680  for (int i = 0; i < _size; i++)
681  {
682  list._items[i] = converter(_items[i]);
683  }
684  list._size = _size;
685  return list;
686  }
687 
693  [__DynamicallyInvokable]
694  public void CopyTo(T[] array)
695  {
696  CopyTo(array, 0);
697  }
698 
709  [__DynamicallyInvokable]
710  void ICollection.CopyTo(Array array, int arrayIndex)
711  {
712  if (array != null && array.Rank != 1)
713  {
714  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
715  }
716  try
717  {
718  Array.Copy(_items, 0, array, arrayIndex, _size);
719  }
721  {
722  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
723  }
724  }
725 
739  [__DynamicallyInvokable]
740  public void CopyTo(int index, T[] array, int arrayIndex, int count)
741  {
742  if (_size - index < count)
743  {
744  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
745  }
746  Array.Copy(_items, index, array, arrayIndex, count);
747  }
748 
757  [__DynamicallyInvokable]
758  public void CopyTo(T[] array, int arrayIndex)
759  {
760  Array.Copy(_items, 0, array, arrayIndex, _size);
761  }
762 
763  private void EnsureCapacity(int min)
764  {
765  if (_items.Length < min)
766  {
767  int num = (_items.Length == 0) ? 4 : (_items.Length * 2);
768  if ((uint)num > 2146435071u)
769  {
770  num = 2146435071;
771  }
772  if (num < min)
773  {
774  num = min;
775  }
776  Capacity = num;
777  }
778  }
779 
786  [__DynamicallyInvokable]
787  public bool Exists(Predicate<T> match)
788  {
789  return FindIndex(match) != -1;
790  }
791 
797  [__DynamicallyInvokable]
798  public T Find(Predicate<T> match)
799  {
800  if (match == null)
801  {
802  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
803  }
804  for (int i = 0; i < _size; i++)
805  {
806  if (match(_items[i]))
807  {
808  return _items[i];
809  }
810  }
811  return default(T);
812  }
813 
819  [__DynamicallyInvokable]
820  public List<T> FindAll(Predicate<T> match)
821  {
822  if (match == null)
823  {
824  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
825  }
826  List<T> list = new List<T>();
827  for (int i = 0; i < _size; i++)
828  {
829  if (match(_items[i]))
830  {
831  list.Add(_items[i]);
832  }
833  }
834  return list;
835  }
836 
842  [__DynamicallyInvokable]
843  public int FindIndex(Predicate<T> match)
844  {
845  return FindIndex(0, _size, match);
846  }
847 
856  [__DynamicallyInvokable]
857  public int FindIndex(int startIndex, Predicate<T> match)
858  {
859  return FindIndex(startIndex, _size - startIndex, match);
860  }
861 
873  [__DynamicallyInvokable]
874  public int FindIndex(int startIndex, int count, Predicate<T> match)
875  {
876  if ((uint)startIndex > (uint)_size)
877  {
878  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
879  }
880  if (count < 0 || startIndex > _size - count)
881  {
882  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
883  }
884  if (match == null)
885  {
886  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
887  }
888  int num = startIndex + count;
889  for (int i = startIndex; i < num; i++)
890  {
891  if (match(_items[i]))
892  {
893  return i;
894  }
895  }
896  return -1;
897  }
898 
904  [__DynamicallyInvokable]
905  public T FindLast(Predicate<T> match)
906  {
907  if (match == null)
908  {
909  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
910  }
911  for (int num = _size - 1; num >= 0; num--)
912  {
913  if (match(_items[num]))
914  {
915  return _items[num];
916  }
917  }
918  return default(T);
919  }
920 
926  [__DynamicallyInvokable]
927  public int FindLastIndex(Predicate<T> match)
928  {
929  return FindLastIndex(_size - 1, _size, match);
930  }
931 
940  [__DynamicallyInvokable]
941  public int FindLastIndex(int startIndex, Predicate<T> match)
942  {
943  return FindLastIndex(startIndex, startIndex + 1, match);
944  }
945 
957  [__DynamicallyInvokable]
958  public int FindLastIndex(int startIndex, int count, Predicate<T> match)
959  {
960  if (match == null)
961  {
962  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
963  }
964  if (_size == 0)
965  {
966  if (startIndex != -1)
967  {
968  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
969  }
970  }
971  else if ((uint)startIndex >= (uint)_size)
972  {
973  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
974  }
975  if (count < 0 || startIndex - count + 1 < 0)
976  {
977  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
978  }
979  int num = startIndex - count;
980  for (int num2 = startIndex; num2 > num; num2--)
981  {
982  if (match(_items[num2]))
983  {
984  return num2;
985  }
986  }
987  return -1;
988  }
989 
995  [__DynamicallyInvokable]
996  public void ForEach(Action<T> action)
997  {
998  if (action == null)
999  {
1000  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
1001  }
1002  int version = _version;
1003  for (int i = 0; i < _size; i++)
1004  {
1005  if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
1006  {
1007  break;
1008  }
1009  action(_items[i]);
1010  }
1011  if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
1012  {
1013  ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
1014  }
1015  }
1016 
1019  [__DynamicallyInvokable]
1020  public Enumerator GetEnumerator()
1021  {
1022  return new Enumerator(this);
1023  }
1024 
1027  [__DynamicallyInvokable]
1029  {
1030  return new Enumerator(this);
1031  }
1032 
1035  [__DynamicallyInvokable]
1037  {
1038  return new Enumerator(this);
1039  }
1040 
1050  [__DynamicallyInvokable]
1051  public List<T> GetRange(int index, int count)
1052  {
1053  if (index < 0)
1054  {
1055  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1056  }
1057  if (count < 0)
1058  {
1059  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1060  }
1061  if (_size - index < count)
1062  {
1063  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
1064  }
1065  List<T> list = new List<T>(count);
1066  Array.Copy(_items, index, list._items, 0, count);
1067  list._size = count;
1068  return list;
1069  }
1070 
1074  [__DynamicallyInvokable]
1075  public int IndexOf(T item)
1076  {
1077  return Array.IndexOf(_items, item, 0, _size);
1078  }
1079 
1085  [__DynamicallyInvokable]
1086  int IList.IndexOf(object item)
1087  {
1088  if (IsCompatibleObject(item))
1089  {
1090  return IndexOf((T)item);
1091  }
1092  return -1;
1093  }
1094 
1101  [__DynamicallyInvokable]
1102  public int IndexOf(T item, int index)
1103  {
1104  if (index > _size)
1105  {
1106  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
1107  }
1108  return Array.IndexOf(_items, item, index, _size - index);
1109  }
1110 
1120  [__DynamicallyInvokable]
1121  public int IndexOf(T item, int index, int count)
1122  {
1123  if (index > _size)
1124  {
1125  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
1126  }
1127  if (count < 0 || index > _size - count)
1128  {
1129  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
1130  }
1131  return Array.IndexOf(_items, item, index, count);
1132  }
1133 
1140  [__DynamicallyInvokable]
1141  public void Insert(int index, T item)
1142  {
1143  if ((uint)index > (uint)_size)
1144  {
1145  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
1146  }
1147  if (_size == _items.Length)
1148  {
1149  EnsureCapacity(_size + 1);
1150  }
1151  if (index < _size)
1152  {
1153  Array.Copy(_items, index, _items, index + 1, _size - index);
1154  }
1155  _items[index] = item;
1156  _size++;
1157  _version++;
1158  }
1159 
1167  [__DynamicallyInvokable]
1168  void IList.Insert(int index, object item)
1169  {
1170  ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
1171  try
1172  {
1173  Insert(index, (T)item);
1174  }
1175  catch (InvalidCastException)
1176  {
1177  ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
1178  }
1179  }
1180 
1189  [__DynamicallyInvokable]
1190  public void InsertRange(int index, IEnumerable<T> collection)
1191  {
1192  if (collection == null)
1193  {
1194  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
1195  }
1196  if ((uint)index > (uint)_size)
1197  {
1198  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
1199  }
1200  ICollection<T> collection2 = collection as ICollection<T>;
1201  if (collection2 != null)
1202  {
1203  int count = collection2.Count;
1204  if (count > 0)
1205  {
1206  EnsureCapacity(_size + count);
1207  if (index < _size)
1208  {
1209  Array.Copy(_items, index, _items, index + count, _size - index);
1210  }
1211  if (this == collection2)
1212  {
1213  Array.Copy(_items, 0, _items, index, index);
1214  Array.Copy(_items, index + count, _items, index * 2, _size - index);
1215  }
1216  else
1217  {
1218  T[] array = new T[count];
1219  collection2.CopyTo(array, 0);
1220  array.CopyTo(_items, index);
1221  }
1222  _size += count;
1223  }
1224  }
1225  else
1226  {
1227  using (IEnumerator<T> enumerator = collection.GetEnumerator())
1228  {
1229  while (enumerator.MoveNext())
1230  {
1231  Insert(index++, enumerator.Current);
1232  }
1233  }
1234  }
1235  _version++;
1236  }
1237 
1241  [__DynamicallyInvokable]
1242  public int LastIndexOf(T item)
1243  {
1244  if (_size == 0)
1245  {
1246  return -1;
1247  }
1248  return LastIndexOf(item, _size - 1, _size);
1249  }
1250 
1257  [__DynamicallyInvokable]
1258  public int LastIndexOf(T item, int index)
1259  {
1260  if (index >= _size)
1261  {
1262  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
1263  }
1264  return LastIndexOf(item, index, index + 1);
1265  }
1266 
1276  [__DynamicallyInvokable]
1277  public int LastIndexOf(T item, int index, int count)
1278  {
1279  if (Count != 0 && index < 0)
1280  {
1281  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1282  }
1283  if (Count != 0 && count < 0)
1284  {
1285  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1286  }
1287  if (_size == 0)
1288  {
1289  return -1;
1290  }
1291  if (index >= _size)
1292  {
1293  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
1294  }
1295  if (count > index + 1)
1296  {
1297  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
1298  }
1299  return Array.LastIndexOf(_items, item, index, count);
1300  }
1301 
1306  [__DynamicallyInvokable]
1307  public bool Remove(T item)
1308  {
1309  int num = IndexOf(item);
1310  if (num >= 0)
1311  {
1312  RemoveAt(num);
1313  return true;
1314  }
1315  return false;
1316  }
1317 
1322  [__DynamicallyInvokable]
1323  void IList.Remove(object item)
1324  {
1325  if (IsCompatibleObject(item))
1326  {
1327  Remove((T)item);
1328  }
1329  }
1330 
1336  [__DynamicallyInvokable]
1337  public int RemoveAll(Predicate<T> match)
1338  {
1339  if (match == null)
1340  {
1341  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
1342  }
1343  int i;
1344  for (i = 0; i < _size && !match(_items[i]); i++)
1345  {
1346  }
1347  if (i >= _size)
1348  {
1349  return 0;
1350  }
1351  int j = i + 1;
1352  while (j < _size)
1353  {
1354  for (; j < _size && match(_items[j]); j++)
1355  {
1356  }
1357  if (j < _size)
1358  {
1359  _items[i++] = _items[j++];
1360  }
1361  }
1362  Array.Clear(_items, i, _size - i);
1363  int result = _size - i;
1364  _size = i;
1365  _version++;
1366  return result;
1367  }
1368 
1374  [__DynamicallyInvokable]
1375  public void RemoveAt(int index)
1376  {
1377  if ((uint)index >= (uint)_size)
1378  {
1379  ThrowHelper.ThrowArgumentOutOfRangeException();
1380  }
1381  _size--;
1382  if (index < _size)
1383  {
1384  Array.Copy(_items, index + 1, _items, index, _size - index);
1385  }
1386  _items[_size] = default(T);
1387  _version++;
1388  }
1389 
1398  [__DynamicallyInvokable]
1399  public void RemoveRange(int index, int count)
1400  {
1401  if (index < 0)
1402  {
1403  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1404  }
1405  if (count < 0)
1406  {
1407  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1408  }
1409  if (_size - index < count)
1410  {
1411  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
1412  }
1413  if (count > 0)
1414  {
1415  int size = _size;
1416  _size -= count;
1417  if (index < _size)
1418  {
1419  Array.Copy(_items, index + count, _items, index, _size - index);
1420  }
1421  Array.Clear(_items, _size, count);
1422  _version++;
1423  }
1424  }
1425 
1427  [__DynamicallyInvokable]
1428  public void Reverse()
1429  {
1430  Reverse(0, Count);
1431  }
1432 
1441  [__DynamicallyInvokable]
1442  public void Reverse(int index, int count)
1443  {
1444  if (index < 0)
1445  {
1446  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1447  }
1448  if (count < 0)
1449  {
1450  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1451  }
1452  if (_size - index < count)
1453  {
1454  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
1455  }
1456  Array.Reverse(_items, index, count);
1457  _version++;
1458  }
1459 
1462  [__DynamicallyInvokable]
1463  public void Sort()
1464  {
1465  Sort(0, Count, null);
1466  }
1467 
1473  [__DynamicallyInvokable]
1474  public void Sort(IComparer<T> comparer)
1475  {
1476  Sort(0, Count, comparer);
1477  }
1478 
1490  [__DynamicallyInvokable]
1491  public void Sort(int index, int count, IComparer<T> comparer)
1492  {
1493  if (index < 0)
1494  {
1495  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1496  }
1497  if (count < 0)
1498  {
1499  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
1500  }
1501  if (_size - index < count)
1502  {
1503  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
1504  }
1505  Array.Sort(_items, index, count, comparer);
1506  _version++;
1507  }
1508 
1514  [__DynamicallyInvokable]
1515  public void Sort(Comparison<T> comparison)
1516  {
1517  if (comparison == null)
1518  {
1519  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
1520  }
1521  if (_size > 0)
1522  {
1523  IComparer<T> comparer = new Array.FunctorComparer<T>(comparison);
1524  Array.Sort(_items, 0, _size, comparer);
1525  }
1526  }
1527 
1530  [__DynamicallyInvokable]
1531  public T[] ToArray()
1532  {
1533  T[] array = new T[_size];
1534  Array.Copy(_items, 0, array, 0, _size);
1535  return array;
1536  }
1537 
1539  [__DynamicallyInvokable]
1540  public void TrimExcess()
1541  {
1542  int num = (int)((double)_items.Length * 0.9);
1543  if (_size < num)
1544  {
1545  Capacity = _size;
1546  }
1547  }
1548 
1555  [__DynamicallyInvokable]
1556  public bool TrueForAll(Predicate<T> match)
1557  {
1558  if (match == null)
1559  {
1560  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
1561  }
1562  for (int i = 0; i < _size; i++)
1563  {
1564  if (!match(_items[i]))
1565  {
1566  return false;
1567  }
1568  }
1569  return true;
1570  }
1571 
1572  internal static IList<T> Synchronized(List<T> list)
1573  {
1574  return new SynchronizedList(list);
1575  }
1576  }
1577 }
int LastIndexOf(T item, int index)
Searches for the specified object and returns the zero-based index of the last occurrence within the ...
Definition: List.cs:1258
Provides a base class for implementations of the T:System.Collections.Generic.IEqualityComparer`1 gen...
void Reset()
Sets the enumerator to its initial position, which is before the first element in the collection.
List(int capacity)
Initializes a new instance of the T:System.Collections.Generic.List`1 class that is empty and has the...
Definition: List.cs:443
int BinarySearch(T item, IComparer< T > comparer)
Searches the entire sorted T:System.Collections.Generic.List`1 for an element using the specified com...
Definition: List.cs:607
bool MoveNext()
Advances the enumerator to the next element of the T:System.Collections.Generic.List`1.
Definition: List.cs:202
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
Definition: List.cs:296
Provides the base class for a generic read-only collection.
void RemoveAt(int index)
Removes the element at the specified index of the T:System.Collections.Generic.List`1.
Definition: List.cs:1375
static void Clear(Array array, int index, int length)
Sets a range of elements in an array to the default value of each element type.
List(IEnumerable< T > collection)
Initializes a new instance of the T:System.Collections.Generic.List`1 class that contains elements co...
Definition: List.cs:464
int IndexOf(T item, int index)
Searches for the specified object and returns the zero-based index of the first occurrence within the...
Definition: List.cs:1102
void InsertRange(int index, IEnumerable< T > collection)
Inserts the elements of a collection into the T:System.Collections.Generic.List`1 at the specified in...
Definition: List.cs:1190
ReadOnlyCollection< T > AsReadOnly()
Returns a read-only T:System.Collections.ObjectModel.ReadOnlyCollection`1 wrapper for the current col...
Definition: List.cs:553
List< TOutput > ConvertAll< TOutput >(Converter< T, TOutput > converter)
Converts the elements in the current T:System.Collections.Generic.List`1 to another type,...
Definition: List.cs:673
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
int FindIndex(int startIndex, int count, Predicate< T > match)
Searches for an element that matches the conditions defined by the specified predicate,...
Definition: List.cs:874
static void Reverse(Array array)
Reverses the sequence of the elements in the entire one-dimensional T:System.Array.
Definition: Array.cs:3171
bool TrueForAll(Predicate< T > match)
Determines whether every element in the T:System.Collections.Generic.List`1 matches the conditions de...
Definition: List.cs:1556
Definition: __Canon.cs:3
new IEnumerator< T > GetEnumerator()
Returns an enumerator that iterates through the collection.
The exception that is thrown for invalid casting or explicit conversion.
int BinarySearch(int index, int count, T item, IComparer< T > comparer)
Searches a range of elements in the sorted T:System.Collections.Generic.List`1 for an element using t...
Definition: List.cs:572
List()
Initializes a new instance of the T:System.Collections.Generic.List`1 class that is empty and has the...
Definition: List.cs:433
bool Remove(T item)
Removes the first occurrence of a specific object from the T:System.Collections.Generic....
int FindLastIndex(int startIndex, Predicate< T > match)
Searches for an element that matches the conditions defined by the specified predicate,...
Definition: List.cs:941
Exposes the enumerator, which supports a simple iteration over a collection of a specified type....
Definition: IEnumerable.cs:9
Represents a strongly-typed, read-only collection of elements.
Represents a read-only collection of elements that can be accessed by index.
Definition: IReadOnlyList.cs:9
void CopyTo(T[] array, int arrayIndex)
Copies the elements of the T:System.Collections.Generic.ICollection`1 to an T:System....
void Insert(int index, T item)
Inserts an item to the T:System.Collections.Generic.IList`1 at the specified index.
int FindLastIndex(Predicate< T > match)
Searches for an element that matches the conditions defined by the specified predicate,...
Definition: List.cs:927
static EqualityComparer< T > Default
Returns a default equality comparer for the type specified by the generic argument.
Supports a simple iteration over a generic collection.
Definition: IEnumerator.cs:6
Defines methods to manipulate generic collections.
Definition: ICollection.cs:9
int FindIndex(Predicate< T > match)
Searches for an element that matches the conditions defined by the specified predicate,...
Definition: List.cs:843
void Sort(Comparison< T > comparison)
Sorts the elements in the entire T:System.Collections.Generic.List`1 using the specified T:System....
Definition: List.cs:1515
int IndexOf(T item, int index, int count)
Searches for the specified object and returns the zero-based index of the first occurrence within the...
Definition: List.cs:1121
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
Definition: List.cs:510
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
T FindLast(Predicate< T > match)
Searches for an element that matches the conditions defined by the specified predicate,...
Definition: List.cs:905
static void Sort(Array array)
Sorts the elements in an entire one-dimensional T:System.Array using the T:System....
Definition: Array.cs:3259
Enumerator GetEnumerator()
Returns an enumerator that iterates through the T:System.Collections.Generic.List`1.
Definition: List.cs:1020
void Insert(int index, T item)
Inserts an element into the T:System.Collections.Generic.List`1 at the specified index.
Definition: List.cs:1141
bool Contains(T item)
Determines whether the T:System.Collections.Generic.ICollection`1 contains a specific value.
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
The exception that is thrown when an attempt is made to store an element of the wrong type within an ...
void Sort(IComparer< T > comparer)
Sorts the elements in the entire T:System.Collections.Generic.List`1 using the specified comparer.
Definition: List.cs:1474
bool IsReadOnly
Gets a value indicating whether the T:System.Collections.Generic.ICollection`1 is read-only.
Definition: ICollection.cs:25
List< T > GetRange(int index, int count)
Creates a shallow copy of a range of elements in the source T:System.Collections.Generic....
Definition: List.cs:1051
T Find(Predicate< T > match)
Searches for an element that matches the conditions defined by the specified predicate,...
Definition: List.cs:798
List< T > FindAll(Predicate< T > match)
Retrieves all the elements that match the conditions defined by the specified predicate.
Definition: List.cs:820
void Dispose()
Releases all resources used by the T:System.Collections.Generic.List`1.Enumerator.
Definition: List.cs:193
void Sort()
Sorts the elements in the entire T:System.Collections.Generic.List`1 using the default comparer.
Definition: List.cs:1463
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
int FindIndex(int startIndex, Predicate< T > match)
Searches for an element that matches the conditions defined by the specified predicate,...
Definition: List.cs:857
void Sort(int index, int count, IComparer< T > comparer)
Sorts the elements in a range of elements in T:System.Collections.Generic.List`1 using the specified ...
Definition: List.cs:1491
new T Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:12
int IndexOf(T item)
Determines the index of a specific item in the T:System.Collections.Generic.IList`1.
bool Contains(T item)
Determines whether an element is in the T:System.Collections.Generic.List`1.
Definition: List.cs:629
void AddRange(IEnumerable< T > collection)
Adds the elements of the specified collection to the end of the T:System.Collections....
Definition: List.cs:545
static int BinarySearch(Array array, object value)
Searches an entire one-dimensional sorted array for a specific element, using the T:System....
Definition: Array.cs:2060
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
int LastIndexOf(T item, int index, int count)
Searches for the specified object and returns the zero-based index of the last occurrence within the ...
Definition: List.cs:1277
bool Exists(Predicate< T > match)
Determines whether the T:System.Collections.Generic.List`1 contains elements that match the condition...
Definition: List.cs:787
int BinarySearch(T item)
Searches the entire sorted T:System.Collections.Generic.List`1 for an element using the default compa...
Definition: List.cs:594
Enumerates the elements of a T:System.Collections.Generic.List`1.
Definition: List.cs:144
void CopyTo(T[] array, int arrayIndex)
Copies the entire T:System.Collections.Generic.List`1 to a compatible one-dimensional array,...
Definition: List.cs:758
Represents a collection of objects that can be individually accessed by index.
Definition: IList.cs:9
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
void Reverse()
Reverses the order of the elements in the entire T:System.Collections.Generic.List`1.
Definition: List.cs:1428
void Clear()
Removes all elements from the T:System.Collections.Generic.List`1.
Definition: List.cs:614
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
void RemoveRange(int index, int count)
Removes a range of elements from the T:System.Collections.Generic.List`1.
Definition: List.cs:1399
int IndexOf(T item)
Searches for the specified object and returns the zero-based index of the first occurrence within the...
Definition: List.cs:1075
int Capacity
Gets or sets the total number of elements the internal data structure can hold without resizing.
Definition: List.cs:259
void Reverse(int index, int count)
Reverses the order of the elements in the specified range.
Definition: List.cs:1442
Specifies that the class can be serialized.
void TrimExcess()
Sets the capacity to the actual number of elements in the T:System.Collections.Generic....
Definition: List.cs:1540
int RemoveAll(Predicate< T > match)
Removes all the elements that match the conditions defined by the specified predicate.
Definition: List.cs:1337
static int LastIndexOf(Array array, object value)
Searches for the specified object and returns the index of the last occurrence within the entire one-...
Definition: Array.cs:2950
void CopyTo(int index, T[] array, int arrayIndex, int count)
Copies a range of elements from the T:System.Collections.Generic.List`1 to a compatible one-dimension...
Definition: List.cs:740
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
T Current
Gets the element at the current position of the enumerator.
Definition: List.cs:158
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
T [] ToArray()
Copies the elements of the T:System.Collections.Generic.List`1 to a new array.
Definition: List.cs:1531
int FindLastIndex(int startIndex, int count, Predicate< T > match)
Searches for an element that matches the conditions defined by the specified predicate,...
Definition: List.cs:958
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
int LastIndexOf(T item)
Searches for the specified object and returns the zero-based index of the last occurrence within the ...
Definition: List.cs:1242
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
void CopyTo(T[] array)
Copies the entire T:System.Collections.Generic.List`1 to a compatible one-dimensional array,...
Definition: List.cs:694
void Add(T item)
Adds an item to the T:System.Collections.Generic.ICollection`1.
void ForEach(Action< T > action)
Performs the specified action on each element of the T:System.Collections.Generic....
Definition: List.cs:996
bool Remove(T item)
Removes the first occurrence of a specific object from the T:System.Collections.Generic....
Definition: List.cs:1307