PageRenderTime 62ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/corlib/System.Collections.ObjectModel/ReadOnlyCollection.cs

https://bitbucket.org/danipen/mono
C# | 202 lines | 134 code | 32 blank | 36 comment | 4 complexity | 96a4de4202fcac65c4bf615257ac5396 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Collections.ObjectModel.ReadOnlyCollection
  4. //
  5. // Authors:
  6. // Zoltan Varga (vargaz@gmail.com)
  7. // David Waite (mass@akuma.org)
  8. // Marek Safar (marek.safar@gmail.com)
  9. //
  10. // (C) 2005 Novell, Inc.
  11. // (C) 2005 David Waite
  12. //
  13. //
  14. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  15. // Copyright (C) 2005 David Waite
  16. // Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
  17. //
  18. // Permission is hereby granted, free of charge, to any person obtaining
  19. // a copy of this software and associated documentation files (the
  20. // "Software"), to deal in the Software without restriction, including
  21. // without limitation the rights to use, copy, modify, merge, publish,
  22. // distribute, sublicense, and/or sell copies of the Software, and to
  23. // permit persons to whom the Software is furnished to do so, subject to
  24. // the following conditions:
  25. //
  26. // The above copyright notice and this permission notice shall be
  27. // included in all copies or substantial portions of the Software.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  33. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  34. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  35. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. //
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Runtime.InteropServices;
  40. using System.Diagnostics;
  41. namespace System.Collections.ObjectModel
  42. {
  43. [ComVisible (false)]
  44. [Serializable]
  45. [DebuggerDisplay ("Count={Count}")]
  46. [DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
  47. public class ReadOnlyCollection<T> : IList<T>, IList
  48. #if NET_4_5
  49. , IReadOnlyList<T>
  50. #endif
  51. {
  52. IList <T> list;
  53. public ReadOnlyCollection (IList <T> list)
  54. {
  55. if (list == null)
  56. throw new ArgumentNullException ("list");
  57. this.list = list;
  58. }
  59. void ICollection<T>.Add (T item)
  60. {
  61. throw new NotSupportedException ();
  62. }
  63. void ICollection<T>.Clear ()
  64. {
  65. throw new NotSupportedException ();
  66. }
  67. public bool Contains (T value)
  68. {
  69. return list.Contains (value);
  70. }
  71. public void CopyTo (T [] array, int index)
  72. {
  73. list.CopyTo (array, index);
  74. }
  75. public IEnumerator <T> GetEnumerator ()
  76. {
  77. return list.GetEnumerator ();
  78. }
  79. public int IndexOf (T value)
  80. {
  81. return list.IndexOf (value);
  82. }
  83. void IList<T>.Insert (int index, T item)
  84. {
  85. throw new NotSupportedException ();
  86. }
  87. bool ICollection<T>.Remove (T item)
  88. {
  89. throw new NotSupportedException ();
  90. }
  91. void IList<T>.RemoveAt (int index)
  92. {
  93. throw new NotSupportedException ();
  94. }
  95. public int Count {
  96. get { return list.Count; }
  97. }
  98. protected IList<T> Items {
  99. get { return list; }
  100. }
  101. public T this [int index] {
  102. get { return list [index]; }
  103. }
  104. T IList<T>.this [int index] {
  105. get { return this [index]; }
  106. set { throw new NotSupportedException (); }
  107. }
  108. bool ICollection<T>.IsReadOnly {
  109. get { return true; }
  110. }
  111. #region Not generic interface implementations
  112. void ICollection.CopyTo (Array array, int index)
  113. {
  114. ((ICollection)list).CopyTo (array, index);
  115. }
  116. IEnumerator IEnumerable.GetEnumerator ()
  117. {
  118. return ((IEnumerable) list).GetEnumerator ();
  119. }
  120. int IList.Add (object value)
  121. {
  122. throw new NotSupportedException ();
  123. }
  124. void IList.Clear ()
  125. {
  126. throw new NotSupportedException ();
  127. }
  128. bool IList.Contains (object value)
  129. {
  130. if (CollectionHelpers.IsValidItem<T> (value))
  131. return list.Contains ((T) value);
  132. return false;
  133. }
  134. int IList.IndexOf (object value)
  135. {
  136. if (CollectionHelpers.IsValidItem<T> (value))
  137. return list.IndexOf ((T) value);
  138. return -1;
  139. }
  140. void IList.Insert (int index, object value)
  141. {
  142. throw new NotSupportedException ();
  143. }
  144. void IList.Remove (object value)
  145. {
  146. throw new NotSupportedException ();
  147. }
  148. void IList.RemoveAt (int index)
  149. {
  150. throw new NotSupportedException ();
  151. }
  152. bool ICollection.IsSynchronized {
  153. get { return false; }
  154. }
  155. object ICollection.SyncRoot {
  156. get { return this; }
  157. }
  158. bool IList.IsFixedSize {
  159. get { return true; }
  160. }
  161. bool IList.IsReadOnly {
  162. get { return true; }
  163. }
  164. object IList.this [int index] {
  165. get { return list [index]; }
  166. set { throw new NotSupportedException (); }
  167. }
  168. #endregion
  169. }
  170. }