mscorlib(4.0.0.0) API with additions
Collection.cs
2 using System.Diagnostics;
4 using System.Threading;
5 
7 {
10  [Serializable]
11  [ComVisible(false)]
12  [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
13  [DebuggerDisplay("Count = {Count}")]
14  [__DynamicallyInvokable]
16  {
17  private IList<T> items;
18 
19  [NonSerialized]
20  private object _syncRoot;
21 
24  [__DynamicallyInvokable]
25  public int Count
26  {
27  [__DynamicallyInvokable]
28  get
29  {
30  return items.Count;
31  }
32  }
33 
36  [__DynamicallyInvokable]
37  protected IList<T> Items
38  {
39  [__DynamicallyInvokable]
40  get
41  {
42  return items;
43  }
44  }
45 
52  [__DynamicallyInvokable]
53  public T this[int index]
54  {
55  [__DynamicallyInvokable]
56  get
57  {
58  return items[index];
59  }
60  [__DynamicallyInvokable]
61  set
62  {
63  if (items.IsReadOnly)
64  {
65  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
66  }
67  if (index < 0 || index >= items.Count)
68  {
69  ThrowHelper.ThrowArgumentOutOfRangeException();
70  }
71  SetItem(index, value);
72  }
73  }
74 
78  [__DynamicallyInvokable]
79  bool ICollection<T>.IsReadOnly
80  {
81  [__DynamicallyInvokable]
82  get
83  {
84  return items.IsReadOnly;
85  }
86  }
87 
91  [__DynamicallyInvokable]
92  bool ICollection.IsSynchronized
93  {
94  [__DynamicallyInvokable]
95  get
96  {
97  return false;
98  }
99  }
100 
103  [__DynamicallyInvokable]
104  object ICollection.SyncRoot
105  {
106  [__DynamicallyInvokable]
107  get
108  {
109  if (_syncRoot == null)
110  {
111  ICollection collection = items as ICollection;
112  if (collection != null)
113  {
114  _syncRoot = collection.SyncRoot;
115  }
116  else
117  {
118  Interlocked.CompareExchange<object>(ref _syncRoot, new object(), (object)null);
119  }
120  }
121  return _syncRoot;
122  }
123  }
124 
131  [__DynamicallyInvokable]
132  object IList.this[int index]
133  {
134  [__DynamicallyInvokable]
135  get
136  {
137  return items[index];
138  }
139  [__DynamicallyInvokable]
140  set
141  {
142  ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value, ExceptionArgument.value);
143  try
144  {
145  this[index] = (T)value;
146  }
147  catch (InvalidCastException)
148  {
149  ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
150  }
151  }
152  }
153 
157  [__DynamicallyInvokable]
158  bool IList.IsReadOnly
159  {
160  [__DynamicallyInvokable]
161  get
162  {
163  return items.IsReadOnly;
164  }
165  }
166 
170  [__DynamicallyInvokable]
171  bool IList.IsFixedSize
172  {
173  [__DynamicallyInvokable]
174  get
175  {
176  return (items as IList)?.IsFixedSize ?? items.IsReadOnly;
177  }
178  }
179 
181  [__DynamicallyInvokable]
182  public Collection()
183  {
184  items = new List<T>();
185  }
186 
191  [__DynamicallyInvokable]
192  public Collection(IList<T> list)
193  {
194  if (list == null)
195  {
196  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
197  }
198  items = list;
199  }
200 
203  [__DynamicallyInvokable]
204  public void Add(T item)
205  {
206  if (items.IsReadOnly)
207  {
208  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
209  }
210  int count = items.Count;
211  InsertItem(count, item);
212  }
213 
215  [__DynamicallyInvokable]
216  public void Clear()
217  {
218  if (items.IsReadOnly)
219  {
220  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
221  }
222  ClearItems();
223  }
224 
233  [__DynamicallyInvokable]
234  public void CopyTo(T[] array, int index)
235  {
236  items.CopyTo(array, index);
237  }
238 
243  [__DynamicallyInvokable]
244  public bool Contains(T item)
245  {
246  return items.Contains(item);
247  }
248 
251  [__DynamicallyInvokable]
253  {
254  return items.GetEnumerator();
255  }
256 
260  [__DynamicallyInvokable]
261  public int IndexOf(T item)
262  {
263  return items.IndexOf(item);
264  }
265 
272  [__DynamicallyInvokable]
273  public void Insert(int index, T item)
274  {
275  if (items.IsReadOnly)
276  {
277  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
278  }
279  if (index < 0 || index > items.Count)
280  {
281  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
282  }
283  InsertItem(index, item);
284  }
285 
290  [__DynamicallyInvokable]
291  public bool Remove(T item)
292  {
293  if (items.IsReadOnly)
294  {
295  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
296  }
297  int num = items.IndexOf(item);
298  if (num < 0)
299  {
300  return false;
301  }
302  RemoveItem(num);
303  return true;
304  }
305 
311  [__DynamicallyInvokable]
312  public void RemoveAt(int index)
313  {
314  if (items.IsReadOnly)
315  {
316  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
317  }
318  if (index < 0 || index >= items.Count)
319  {
320  ThrowHelper.ThrowArgumentOutOfRangeException();
321  }
322  RemoveItem(index);
323  }
324 
326  [__DynamicallyInvokable]
327  protected virtual void ClearItems()
328  {
329  items.Clear();
330  }
331 
338  [__DynamicallyInvokable]
339  protected virtual void InsertItem(int index, T item)
340  {
341  items.Insert(index, item);
342  }
343 
349  [__DynamicallyInvokable]
350  protected virtual void RemoveItem(int index)
351  {
352  items.RemoveAt(index);
353  }
354 
361  [__DynamicallyInvokable]
362  protected virtual void SetItem(int index, T item)
363  {
364  items[index] = item;
365  }
366 
369  [__DynamicallyInvokable]
371  {
372  return ((IEnumerable)items).GetEnumerator();
373  }
374 
385  [__DynamicallyInvokable]
386  void ICollection.CopyTo(Array array, int index)
387  {
388  if (array == null)
389  {
390  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
391  }
392  if (array.Rank != 1)
393  {
394  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
395  }
396  if (array.GetLowerBound(0) != 0)
397  {
398  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
399  }
400  if (index < 0)
401  {
402  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
403  }
404  if (array.Length - index < Count)
405  {
406  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
407  }
408  T[] array2 = array as T[];
409  if (array2 != null)
410  {
411  items.CopyTo(array2, index);
412  return;
413  }
414  Type elementType = array.GetType().GetElementType();
415  Type typeFromHandle = typeof(T);
416  if (!elementType.IsAssignableFrom(typeFromHandle) && !typeFromHandle.IsAssignableFrom(elementType))
417  {
418  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
419  }
420  object[] array3 = array as object[];
421  if (array3 == null)
422  {
423  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
424  }
425  int count = items.Count;
426  try
427  {
428  for (int i = 0; i < count; i++)
429  {
430  array3[index++] = items[i];
431  }
432  }
433  catch (ArrayTypeMismatchException)
434  {
435  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
436  }
437  }
438 
444  [__DynamicallyInvokable]
445  int IList.Add(object value)
446  {
447  if (items.IsReadOnly)
448  {
449  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
450  }
451  ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value, ExceptionArgument.value);
452  try
453  {
454  Add((T)value);
455  }
456  catch (InvalidCastException)
457  {
458  ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
459  }
460  return Count - 1;
461  }
462 
469  [__DynamicallyInvokable]
470  bool IList.Contains(object value)
471  {
472  if (IsCompatibleObject(value))
473  {
474  return Contains((T)value);
475  }
476  return false;
477  }
478 
484  [__DynamicallyInvokable]
485  int IList.IndexOf(object value)
486  {
487  if (IsCompatibleObject(value))
488  {
489  return IndexOf((T)value);
490  }
491  return -1;
492  }
493 
501  [__DynamicallyInvokable]
502  void IList.Insert(int index, object value)
503  {
504  if (items.IsReadOnly)
505  {
506  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
507  }
508  ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value, ExceptionArgument.value);
509  try
510  {
511  Insert(index, (T)value);
512  }
513  catch (InvalidCastException)
514  {
515  ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
516  }
517  }
518 
523  [__DynamicallyInvokable]
524  void IList.Remove(object value)
525  {
526  if (items.IsReadOnly)
527  {
528  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
529  }
530  if (IsCompatibleObject(value))
531  {
532  Remove((T)value);
533  }
534  }
535 
536  private static bool IsCompatibleObject(object value)
537  {
538  if (!(value is T))
539  {
540  if (value == null)
541  {
542  return default(T) == null;
543  }
544  return false;
545  }
546  return true;
547  }
548  }
549 }
void Clear()
Removes all elements from the T:System.Collections.ObjectModel.Collection`1.
Definition: Collection.cs:216
bool Remove(T item)
Removes the first occurrence of a specific object from the T:System.Collections.ObjectModel....
Definition: Collection.cs:291
virtual void ClearItems()
Removes all elements from the T:System.Collections.ObjectModel.Collection`1.
Definition: Collection.cs:327
int IndexOf(T item)
Searches for the specified object and returns the zero-based index of the first occurrence within the...
Definition: Collection.cs:261
virtual void InsertItem(int index, T item)
Inserts an element into the T:System.Collections.ObjectModel.Collection`1 at the specified index.
Definition: Collection.cs:339
Represents a non-generic collection of objects that can be individually accessed by index.
Definition: IList.cs:8
Definition: __Canon.cs:3
bool Remove(T item)
Removes the first occurrence of a specific object from the T:System.Collections.Generic....
IList< T > Items
Gets a T:System.Collections.Generic.IList`1 wrapper around the T:System.Collections....
Definition: Collection.cs:38
Represents a strongly-typed, read-only collection of elements.
void Insert(int index, T item)
Inserts an element into the T:System.Collections.ObjectModel.Collection`1 at the specified index.
Definition: Collection.cs:273
Represents a read-only collection of elements that can be accessed by index.
Definition: IReadOnlyList.cs:9
void Insert(int index, T item)
Inserts an item to the T:System.Collections.Generic.IList`1 at the specified index.
Defines methods to manipulate generic collections.
Definition: ICollection.cs:9
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
virtual void SetItem(int index, T item)
Replaces the element at the specified index.
Definition: Collection.cs:362
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.
bool IsReadOnly
Gets a value indicating whether the T:System.Collections.Generic.ICollection`1 is read-only.
Definition: ICollection.cs:25
Collection()
Initializes a new instance of the T:System.Collections.ObjectModel.Collection`1 class that is empty.
Definition: Collection.cs:182
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
int Count
Gets the number of elements actually contained in the T:System.Collections.ObjectModel....
Definition: Collection.cs:26
void Add(T item)
Adds an object to the end of the T:System.Collections.ObjectModel.Collection`1.
Definition: Collection.cs:204
Collection(IList< T > list)
Initializes a new instance of the T:System.Collections.ObjectModel.Collection`1 class as a wrapper fo...
Definition: Collection.cs:192
int IndexOf(T item)
Determines the index of a specific item in the T:System.Collections.Generic.IList`1.
void RemoveAt(int index)
Removes the element at the specified index of the T:System.Collections.ObjectModel....
Definition: Collection.cs:312
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
virtual void RemoveItem(int index)
Removes the element at the specified index of the T:System.Collections.ObjectModel....
Definition: Collection.cs:350
void CopyTo(T[] array, int index)
Copies the entire T:System.Collections.ObjectModel.Collection`1 to a compatible one-dimensional T:Sys...
Definition: Collection.cs:234
Represents a collection of objects that can be individually accessed by index.
Definition: IList.cs:9
IEnumerator< T > GetEnumerator()
Returns an enumerator that iterates through the T:System.Collections.ObjectModel.Collection`1.
Definition: Collection.cs:252
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
Provides the base class for a generic collection.
Definition: Collection.cs:15
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
bool Contains(T item)
Determines whether an element is in the T:System.Collections.ObjectModel.Collection`1.
Definition: Collection.cs:244
void Add(T item)
Adds an item to the T:System.Collections.Generic.ICollection`1.