1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 // System.Collections.ObjectModel.Collection
6 // Zoltan Varga (vargaz@gmail.com)
7 // David Waite (mass@akuma.org)
8 // Marek Safar (marek.safar@gmail.com)
10 // (C) 2005 Novell, Inc.
11 // (C) 2005 David Waite
15 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
16 // Copyright (C) 2005 David Waite
17 // Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
19 // Permission is hereby granted, free of charge, to any person obtaining
20 // a copy of this software and associated documentation files (the
21 // "Software"), to deal in the Software without restriction, including
22 // without limitation the rights to use, copy, modify, merge, publish,
23 // distribute, sublicense, and/or sell copies of the Software, and to
24 // permit persons to whom the Software is furnished to do so, subject to
25 // the following conditions:
27 // The above copyright notice and this permission notice shall be
28 // included in all copies or substantial portions of the Software.
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 using System.Collections;
41 using System.Collections.Generic;
42 using System.Runtime.InteropServices;
43 using System.Diagnostics;
45 namespace System.Collections.ObjectModel
49 [DebuggerDisplay ("Count={Count}")]
50 [DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
51 public class Collection<T> : IList<T>, IList
61 List <T> l = new List <T> ();
62 IList l2 = l as IList;
63 syncRoot = l2.SyncRoot;
67 public Collection (IList <T> list)
70 throw new ArgumentNullException ("list");
72 ICollection l = list as ICollection;
73 syncRoot = (l != null) ? l.SyncRoot : new object ();
76 public void Add (T item)
79 InsertItem (idx, item);
87 protected virtual void ClearItems ()
92 public bool Contains (T item)
94 return list.Contains (item);
97 public void CopyTo (T [] array, int index)
99 list.CopyTo (array, index);
102 public IEnumerator <T> GetEnumerator ()
104 return list.GetEnumerator ();
107 public int IndexOf (T item)
109 return list.IndexOf (item);
112 public void Insert (int index, T item)
114 InsertItem (index, item);
117 protected virtual void InsertItem (int index, T item)
119 list.Insert (index, item);
122 protected IList<T> Items {
126 public bool Remove (T item)
128 int idx = IndexOf (item);
137 public void RemoveAt (int index)
142 protected virtual void RemoveItem (int index)
144 list.RemoveAt (index);
148 get { return list.Count; }
151 public T this [int index] {
152 get { return list [index]; }
153 set { SetItem (index, value); }
156 bool ICollection<T>.IsReadOnly {
157 get { return list.IsReadOnly; }
160 protected virtual void SetItem (int index, T item)
166 #region Helper methods for non-generic interfaces
168 internal static T ConvertItem (object item)
170 if (CollectionHelpers.IsValidItem<T> (item))
172 throw new ArgumentException ("item");
175 internal static void CheckWritable (IList <T> list)
178 throw new NotSupportedException ();
181 internal static bool IsSynchronized (IList <T> list)
183 ICollection c = list as ICollection;
184 return (c != null) ? c.IsSynchronized : false;
187 internal static bool IsFixedSize (IList <T> list)
189 IList l = list as IList;
190 return (l != null) ? l.IsFixedSize : false;
194 #region Not generic interface implementations
195 void ICollection.CopyTo (Array array, int index)
197 ((ICollection)list).CopyTo (array, index);
200 IEnumerator IEnumerable.GetEnumerator ()
202 return (IEnumerator) list.GetEnumerator ();
205 int IList.Add (object value)
207 int idx = list.Count;
208 InsertItem (idx, ConvertItem (value));
212 bool IList.Contains (object value)
214 if (CollectionHelpers.IsValidItem<T> (value))
215 return list.Contains ((T) value);
219 int IList.IndexOf (object value)
221 if (CollectionHelpers.IsValidItem<T> (value))
222 return list.IndexOf ((T) value);
226 void IList.Insert (int index, object value)
228 InsertItem (index, ConvertItem (value));
231 void IList.Remove (object value)
233 CheckWritable (list);
235 int idx = IndexOf (ConvertItem (value));
240 bool ICollection.IsSynchronized {
241 get { return IsSynchronized (list); }
244 object ICollection.SyncRoot {
245 get { return syncRoot; }
247 bool IList.IsFixedSize {
248 get { return IsFixedSize (list); }
251 bool IList.IsReadOnly {
252 get { return list.IsReadOnly; }
255 object IList.this [int index] {
256 get { return list [index]; }
257 set { SetItem (index, ConvertItem (value)); }
262 static class CollectionHelpers
264 public static bool IsValidItem<T> (object item)
266 return item is T || (item == null && ! typeof (T).IsValueType);