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)
9 // (C) 2005 Novell, Inc.
10 // (C) 2005 David Waite
14 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
15 // Copyright (C) 2005 David Waite
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System.Collections.Generic;
40 using System.Runtime.InteropServices;
42 namespace System.Collections.ObjectModel
46 public class ReadOnlyCollection <T> : IList <T>, ICollection <T>, IEnumerable <T>, IList, ICollection, IEnumerable
50 public ReadOnlyCollection (IList <T> list)
53 throw new ArgumentNullException ("list");
57 void ICollection<T>.Add (T item)
59 throw new NotSupportedException ();
62 void ICollection<T>.Clear ()
64 throw new NotSupportedException ();
67 public bool Contains (T item)
69 return list.Contains (item);
72 public void CopyTo (T [] array, int index)
74 list.CopyTo (array, index);
77 public IEnumerator <T> GetEnumerator ()
79 return list.GetEnumerator ();
82 public int IndexOf (T item)
84 return list.IndexOf (item);
87 void IList<T>.Insert (int index, T item)
89 throw new NotSupportedException ();
92 bool ICollection<T>.Remove (T item)
94 throw new NotSupportedException ();
97 void IList<T>.RemoveAt (int index)
99 throw new NotSupportedException ();
103 get { return list.Count; }
106 protected IList<T> Items {
110 public T this [int index] {
111 get { return list [index]; }
114 T IList<T>.this [int index] {
115 get { return this [index]; }
116 set { throw new NotSupportedException (); }
119 bool ICollection<T>.IsReadOnly {
123 #region Not generic interface implementations
124 void ICollection.CopyTo (Array array, int arrayIndex)
126 ((ICollection)list).CopyTo (array, arrayIndex);
129 IEnumerator IEnumerable.GetEnumerator ()
131 return ((IEnumerable) list).GetEnumerator ();
134 int IList.Add (object item)
136 throw new NotSupportedException ();
141 throw new NotSupportedException ();
144 bool IList.Contains (object item)
146 if (Collection <T>.IsValidItem (item))
147 return list.Contains ((T) item);
151 int IList.IndexOf (object item)
153 if (Collection <T>.IsValidItem (item))
154 return list.IndexOf ((T) item);
158 void IList.Insert (int index, object item)
160 throw new NotSupportedException ();
163 void IList.Remove (object item)
165 throw new NotSupportedException ();
168 void IList.RemoveAt (int index)
170 throw new NotSupportedException ();
173 bool ICollection.IsSynchronized {
174 get { return false; }
177 object ICollection.SyncRoot {
181 bool IList.IsFixedSize {
185 bool IList.IsReadOnly {
189 object IList.this [int index] {
190 get { return list [index]; }
191 set { throw new NotSupportedException (); }