mscorlib(4.0.0.0) API with additions
CollectionBase.cs
2 
3 namespace System.Collections
4 {
7  [ComVisible(true)]
8  [__DynamicallyInvokable]
9  public abstract class CollectionBase : IList, ICollection, IEnumerable
10  {
11  private ArrayList list;
12 
15  protected ArrayList InnerList
16  {
17  get
18  {
19  if (list == null)
20  {
21  list = new ArrayList();
22  }
23  return list;
24  }
25  }
26 
29  protected IList List => this;
30 
36  [ComVisible(false)]
37  public int Capacity
38  {
39  get
40  {
41  return InnerList.Capacity;
42  }
43  set
44  {
45  InnerList.Capacity = value;
46  }
47  }
48 
51  public int Count
52  {
53  [__DynamicallyInvokable]
54  get
55  {
56  if (list != null)
57  {
58  return list.Count;
59  }
60  return 0;
61  }
62  }
63 
67  bool IList.IsReadOnly
68  {
69  get
70  {
71  return InnerList.IsReadOnly;
72  }
73  }
74 
78  bool IList.IsFixedSize
79  {
80  get
81  {
82  return InnerList.IsFixedSize;
83  }
84  }
85 
89  bool ICollection.IsSynchronized
90  {
91  get
92  {
94  }
95  }
96 
99  object ICollection.SyncRoot
100  {
101  get
102  {
103  return InnerList.SyncRoot;
104  }
105  }
106 
113  object IList.this[int index]
114  {
115  get
116  {
117  if (index < 0 || index >= Count)
118  {
119  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
120  }
121  return InnerList[index];
122  }
123  set
124  {
125  if (index < 0 || index >= Count)
126  {
127  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
128  }
129  OnValidate(value);
130  object obj = InnerList[index];
131  OnSet(index, obj, value);
132  InnerList[index] = value;
133  try
134  {
135  OnSetComplete(index, obj, value);
136  }
137  catch
138  {
139  InnerList[index] = obj;
140  throw;
141  }
142  }
143  }
144 
146  protected CollectionBase()
147  {
148  list = new ArrayList();
149  }
150 
153  protected CollectionBase(int capacity)
154  {
155  list = new ArrayList(capacity);
156  }
157 
159  [__DynamicallyInvokable]
160  public void Clear()
161  {
162  OnClear();
163  InnerList.Clear();
164  OnClearComplete();
165  }
166 
172  [__DynamicallyInvokable]
173  public void RemoveAt(int index)
174  {
175  if (index < 0 || index >= Count)
176  {
177  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
178  }
179  object value = InnerList[index];
180  OnValidate(value);
181  OnRemove(index, value);
182  InnerList.RemoveAt(index);
183  try
184  {
185  OnRemoveComplete(index, value);
186  }
187  catch
188  {
189  InnerList.Insert(index, value);
190  throw;
191  }
192  }
193 
204  void ICollection.CopyTo(Array array, int index)
205  {
206  InnerList.CopyTo(array, index);
207  }
208 
213  bool IList.Contains(object value)
214  {
215  return InnerList.Contains(value);
216  }
217 
222  int IList.Add(object value)
223  {
224  OnValidate(value);
225  OnInsert(InnerList.Count, value);
226  int num = InnerList.Add(value);
227  try
228  {
229  OnInsertComplete(num, value);
230  return num;
231  }
232  catch
233  {
234  InnerList.RemoveAt(num);
235  throw;
236  }
237  }
238 
243  void IList.Remove(object value)
244  {
245  OnValidate(value);
246  int num = InnerList.IndexOf(value);
247  if (num < 0)
248  {
249  throw new ArgumentException(Environment.GetResourceString("Arg_RemoveArgNotFound"));
250  }
251  OnRemove(num, value);
252  InnerList.RemoveAt(num);
253  try
254  {
255  OnRemoveComplete(num, value);
256  }
257  catch
258  {
259  InnerList.Insert(num, value);
260  throw;
261  }
262  }
263 
267  int IList.IndexOf(object value)
268  {
269  return InnerList.IndexOf(value);
270  }
271 
279  void IList.Insert(int index, object value)
280  {
281  if (index < 0 || index > Count)
282  {
283  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
284  }
285  OnValidate(value);
286  OnInsert(index, value);
287  InnerList.Insert(index, value);
288  try
289  {
290  OnInsertComplete(index, value);
291  }
292  catch
293  {
294  InnerList.RemoveAt(index);
295  throw;
296  }
297  }
298 
301  [__DynamicallyInvokable]
303  {
304  return InnerList.GetEnumerator();
305  }
306 
311  protected virtual void OnSet(int index, object oldValue, object newValue)
312  {
313  }
314 
318  protected virtual void OnInsert(int index, object value)
319  {
320  }
321 
323  protected virtual void OnClear()
324  {
325  }
326 
330  protected virtual void OnRemove(int index, object value)
331  {
332  }
333 
338  protected virtual void OnValidate(object value)
339  {
340  if (value == null)
341  {
342  throw new ArgumentNullException("value");
343  }
344  }
345 
350  protected virtual void OnSetComplete(int index, object oldValue, object newValue)
351  {
352  }
353 
357  protected virtual void OnInsertComplete(int index, object value)
358  {
359  }
360 
362  protected virtual void OnClearComplete()
363  {
364  }
365 
369  protected virtual void OnRemoveComplete(int index, object value)
370  {
371  }
372  }
373 }
virtual bool IsFixedSize
Gets a value indicating whether the T:System.Collections.ArrayList has a fixed size.
Definition: ArrayList.cs:2260
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Provides the abstract base class for a strongly typed collection.
virtual void Insert(int index, object value)
Inserts an element into the T:System.Collections.ArrayList at the specified index.
Definition: ArrayList.cs:2698
virtual void RemoveAt(int index)
Removes the element at the specified index of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2847
virtual void OnClearComplete()
Performs additional custom processes after clearing the contents of the T:System.Collections....
Represents a non-generic collection of objects that can be individually accessed by index.
Definition: IList.cs:8
void RemoveAt(int index)
Removes the element at the specified index of the T:System.Collections.CollectionBase instance....
virtual void OnSet(int index, object oldValue, object newValue)
Performs additional custom processes before setting a value in the T:System.Collections....
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
virtual void OnRemoveComplete(int index, object value)
Performs additional custom processes after removing an element from the T:System.Collections....
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
bool IsReadOnly
Gets a value indicating whether the T:System.Collections.IList is read-only.
Definition: IList.cs:30
IEnumerator GetEnumerator()
Returns an enumerator that iterates through the T:System.Collections.CollectionBase instance.
virtual void Clear()
Removes all elements from the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2461
virtual void OnInsertComplete(int index, object value)
Performs additional custom processes after inserting a new element into the T:System....
ArrayList InnerList
Gets an T:System.Collections.ArrayList containing the list of elements in the T:System....
virtual bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.ArrayList is synchronized (thread ...
Definition: ArrayList.cs:2270
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual bool IsReadOnly
Gets a value indicating whether the T:System.Collections.ArrayList is read-only.
Definition: ArrayList.cs:2265
virtual bool Contains(object item)
Determines whether an element is in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2486
IList List
Gets an T:System.Collections.IList containing the list of elements in the T:System....
int Capacity
Gets or sets the number of elements that the T:System.Collections.CollectionBase can contain.
virtual void OnClear()
Performs additional custom processes when clearing the contents of the T:System.Collections....
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
virtual void CopyTo(Array array)
Copies the entire T:System.Collections.ArrayList to a compatible one-dimensional T:System....
Definition: ArrayList.cs:2516
virtual object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: ArrayList.cs:2275
CollectionBase()
Initializes a new instance of the T:System.Collections.CollectionBase class with the default initial ...
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
CollectionBase(int capacity)
Initializes a new instance of the T:System.Collections.CollectionBase class with the specified capaci...
virtual void OnValidate(object value)
Performs additional custom processes when validating a value.
int Count
Gets the number of elements contained in the T:System.Collections.CollectionBase instance....
virtual void OnSetComplete(int index, object oldValue, object newValue)
Performs additional custom processes after setting a value in the T:System.Collections....
virtual void OnInsert(int index, object value)
Performs additional custom processes before inserting a new element into the T:System....
void Clear()
Removes all objects from the T:System.Collections.CollectionBase instance. This method cannot be over...
Specifies that the class can be serialized.
virtual void OnRemove(int index, object value)
Performs additional custom processes when removing an element from the T:System.Collections....
virtual IEnumerator GetEnumerator()
Returns an enumerator for the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:2615
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
virtual int IndexOf(object value)
Searches for the specified T:System.Object and returns the zero-based index of the first occurrence w...
Definition: ArrayList.cs:2649
virtual int Capacity
Gets or sets the number of elements that the T:System.Collections.ArrayList can contain.
Definition: ArrayList.cs:2222
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14