2 // SynchronizedCollection.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.Runtime.InteropServices;
34 namespace System.Collections.Generic
36 [ComVisibleAttribute (false)]
37 public class SynchronizedCollection<T> : IList<T>, ICollection<T>,
38 IEnumerable<T>, IList, ICollection, IEnumerable
43 public SynchronizedCollection ()
44 : this (new object (), null, false)
48 public SynchronizedCollection (object syncRoot)
49 : this (syncRoot, null, false)
53 public SynchronizedCollection (object syncRoot,
55 : this (syncRoot, new List<T> (list), false)
59 public SynchronizedCollection (object syncRoot,
61 : this (syncRoot, new List<T> (list), false)
65 public SynchronizedCollection (object syncRoot,
66 List<T> list, bool makeCopy)
69 syncRoot = new object ();
72 this.list = new List<T> ();
74 this.list = new List<T> (list);
87 public T this [int index] {
94 SetItem (index, value);
98 public object SyncRoot {
102 protected List<T> Items {
106 public void Add (T item)
108 InsertItem (list.Count, item);
116 public bool Contains (T item)
119 return list.Contains (item);
123 public void CopyTo (T [] array, int index)
126 list.CopyTo (array, index);
130 [MonoTODO ("Should be synchronized enumerator?")]
131 public IEnumerator<T> GetEnumerator ()
134 return list.GetEnumerator ();
138 public int IndexOf (T item)
141 return list.IndexOf (item);
145 public void Insert (int index, T item)
147 InsertItem (index, item);
150 [MonoTODO ("should we lock and remove item without invoking RemoveItem() instead?")]
151 public bool Remove (T item)
153 int index = IndexOf (item);
160 public void RemoveAt (int index)
165 protected virtual void ClearItems ()
172 protected virtual void InsertItem (int index, T item)
175 list.Insert (index, item);
179 protected virtual void RemoveItem (int index)
182 list.RemoveAt (index);
186 protected virtual void SetItem (int index, T item)
193 #region Explicit interface implementations
195 void ICollection.CopyTo (Array array, int index)
197 CopyTo ((T []) array, index);
200 IEnumerator IEnumerable.GetEnumerator ()
202 return GetEnumerator ();
205 int IList.Add (object value)
209 return list.Count - 1;
213 bool IList.Contains (object value)
215 return Contains ((T) value);
218 int IList.IndexOf (object value)
220 return IndexOf ((T) value);
223 void IList.Insert (int index, object value)
225 Insert (index, (T) value);
228 void IList.Remove (object value)
233 bool ICollection<T>.IsReadOnly {
234 get { return false; }
237 bool ICollection.IsSynchronized {
241 object ICollection.SyncRoot {
245 bool IList.IsFixedSize {
246 get { return false; }
249 bool IList.IsReadOnly {
250 get { return false; }
253 object IList.this [int index] {
254 get { return this [index]; }
255 set { this [index] = (T) value; }