1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 // System.Collections.ObjectModel.ReadOnlyCollection
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.Generic;
41 using System.Runtime.InteropServices;
42 using System.Diagnostics;
44 namespace System.Collections.ObjectModel
48 [DebuggerDisplay ("Count={Count}")]
49 [DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
50 public class ReadOnlyCollection<T> : IList<T>, IList
57 public ReadOnlyCollection (IList <T> list)
60 throw new ArgumentNullException ("list");
64 void ICollection<T>.Add (T item)
66 throw new NotSupportedException ();
69 void ICollection<T>.Clear ()
71 throw new NotSupportedException ();
74 public bool Contains (T value)
76 return list.Contains (value);
79 public void CopyTo (T [] array, int index)
81 list.CopyTo (array, index);
84 public IEnumerator <T> GetEnumerator ()
86 return list.GetEnumerator ();
89 public int IndexOf (T value)
91 return list.IndexOf (value);
94 void IList<T>.Insert (int index, T item)
96 throw new NotSupportedException ();
99 bool ICollection<T>.Remove (T item)
101 throw new NotSupportedException ();
104 void IList<T>.RemoveAt (int index)
106 throw new NotSupportedException ();
110 get { return list.Count; }
113 protected IList<T> Items {
117 public T this [int index] {
118 get { return list [index]; }
121 T IList<T>.this [int index] {
122 get { return this [index]; }
123 set { throw new NotSupportedException (); }
126 bool ICollection<T>.IsReadOnly {
130 #region Not generic interface implementations
131 void ICollection.CopyTo (Array array, int index)
133 ((ICollection)list).CopyTo (array, index);
136 IEnumerator IEnumerable.GetEnumerator ()
138 return ((IEnumerable) list).GetEnumerator ();
141 int IList.Add (object value)
143 throw new NotSupportedException ();
148 throw new NotSupportedException ();
151 bool IList.Contains (object value)
153 if (CollectionHelpers.IsValidItem<T> (value))
154 return list.Contains ((T) value);
158 int IList.IndexOf (object value)
160 if (CollectionHelpers.IsValidItem<T> (value))
161 return list.IndexOf ((T) value);
165 void IList.Insert (int index, object value)
167 throw new NotSupportedException ();
170 void IList.Remove (object value)
172 throw new NotSupportedException ();
175 void IList.RemoveAt (int index)
177 throw new NotSupportedException ();
180 bool ICollection.IsSynchronized {
181 get { return false; }
184 object ICollection.SyncRoot {
188 bool IList.IsFixedSize {
192 bool IList.IsReadOnly {
196 object IList.this [int index] {
197 get { return list [index]; }
198 set { throw new NotSupportedException (); }