Export (0) Print
Expand All

ReadOnlyCollectionBase Class

Provides the abstract base class for a strongly typed non-generic read-only collection.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class ReadOnlyCollectionBase : ICollection, 
	IEnumerable

A ReadOnlyCollectionBase instance is always read-only. See CollectionBase for a modifiable version of this class.

Notes to Implementers:

This base class is provided to make it easier for implementers to create a strongly typed read-only custom collection. Implementers are encouraged to extend this base class instead of creating their own. Members of this base class are protected and are intended to be used through a derived class only.

This class makes the underlying collection available through the InnerList property, which is intended for use only by classes that are derived directly from ReadOnlyCollectionBase. The derived class must ensure that its own users cannot modify the underlying collection.

The following code example implements the ReadOnlyCollectionBase class.

using System;
using System.Collections;

public class ROCollection : ReadOnlyCollectionBase  {

   public ROCollection( IList sourceList )  {
      InnerList.AddRange( sourceList );
   }

   public Object this[ int index ]  {
      get  {
         return( InnerList[index] );
      }
   }

   public int IndexOf( Object value )  {
      return( InnerList.IndexOf( value ) );
   }

   public bool Contains( Object value )  {
      return( InnerList.Contains( value ) );
   }

}


public class SamplesCollectionBase  {

   public static void Main()  {

      // Create an ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "red" );
      myAL.Add( "blue" );
      myAL.Add( "yellow" );
      myAL.Add( "green" );
      myAL.Add( "orange" );
      myAL.Add( "purple" );

      // Create a new ROCollection that contains the elements in myAL.
      ROCollection myCol = new ROCollection( myAL );

      // Display the contents of the collection using foreach. This is the preferred method.
      Console.WriteLine( "Contents of the collection (using foreach):" );
      PrintValues1( myCol );

      // Display the contents of the collection using the enumerator.
      Console.WriteLine( "Contents of the collection (using enumerator):" );
      PrintValues2( myCol );

      // Display the contents of the collection using the Count property and the Item property.
      Console.WriteLine( "Contents of the collection (using Count and Item):" );
      PrintIndexAndValues( myCol );

      // Search the collection with Contains and IndexOf.
      Console.WriteLine( "Contains yellow: {0}", myCol.Contains( "yellow" ) );
      Console.WriteLine( "orange is at index {0}.", myCol.IndexOf( "orange" ) );
      Console.WriteLine();

   }

   // Uses the Count property and the Item property. 
   public static void PrintIndexAndValues( ROCollection myCol )  {
      for ( int i = 0; i < myCol.Count; i++ )
         Console.WriteLine( "   [{0}]:   {1}", i, myCol[i] );
      Console.WriteLine();
   }

   // Uses the foreach statement which hides the complexity of the enumerator. 
   // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. 
   public static void PrintValues1( ROCollection myCol )  {
      foreach ( Object obj in myCol )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }

   // Uses the enumerator.  
   // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. 
   public static void PrintValues2( ROCollection myCol )  {
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   {0}", myEnumerator.Current );
      Console.WriteLine();
   }

}


/* 
This code produces the following output.

Contents of the collection (using foreach):
   red
   blue
   yellow
   green
   orange
   purple

Contents of the collection (using enumerator):
   red
   blue
   yellow
   green
   orange
   purple

Contents of the collection (using Count and Item):
   [0]:   red
   [1]:   blue
   [2]:   yellow
   [3]:   green
   [4]:   orange
   [5]:   purple

Contains yellow: True
orange is at index 4.

*/

System.Object
  System.Collections.ReadOnlyCollectionBase
    System.ComponentModel.ComponentCollection
    System.Configuration.ConfigurationLocationCollection
    System.Diagnostics.ProcessModuleCollection
    System.Diagnostics.ProcessThreadCollection
    System.DirectoryServices.ActiveDirectory.ActiveDirectoryRoleCollection
    System.DirectoryServices.ActiveDirectory.AdamInstanceCollection
    System.DirectoryServices.ActiveDirectory.AdamRoleCollection
    System.DirectoryServices.ActiveDirectory.ApplicationPartitionCollection
    System.DirectoryServices.ActiveDirectory.AttributeMetadataCollection
    System.DirectoryServices.ActiveDirectory.DomainCollection
    System.DirectoryServices.ActiveDirectory.DomainControllerCollection
    System.DirectoryServices.ActiveDirectory.ForestTrustDomainInfoCollection
    System.DirectoryServices.ActiveDirectory.ForestTrustRelationshipCollisionCollection
    System.DirectoryServices.ActiveDirectory.GlobalCatalogCollection
    System.DirectoryServices.ActiveDirectory.ReadOnlyActiveDirectorySchemaClassCollection
    System.DirectoryServices.ActiveDirectory.ReadOnlyActiveDirectorySchemaPropertyCollection
    System.DirectoryServices.ActiveDirectory.ReadOnlyDirectoryServerCollection
    System.DirectoryServices.ActiveDirectory.ReadOnlySiteCollection
    System.DirectoryServices.ActiveDirectory.ReadOnlySiteLinkBridgeCollection
    System.DirectoryServices.ActiveDirectory.ReadOnlySiteLinkCollection
    System.DirectoryServices.ActiveDirectory.ReadOnlyStringCollection
    System.DirectoryServices.ActiveDirectory.ReplicationConnectionCollection
    System.DirectoryServices.ActiveDirectory.ReplicationCursorCollection
    System.DirectoryServices.ActiveDirectory.ReplicationFailureCollection
    System.DirectoryServices.ActiveDirectory.ReplicationNeighborCollection
    System.DirectoryServices.ActiveDirectory.ReplicationOperationCollection
    System.DirectoryServices.ActiveDirectory.TopLevelNameCollection
    System.DirectoryServices.ActiveDirectory.TrustRelationshipInformationCollection
    System.DirectoryServices.Protocols.PartialResultsCollection
    System.DirectoryServices.Protocols.SearchResultEntryCollection
    System.DirectoryServices.Protocols.SearchResultReferenceCollection
    System.DirectoryServices.ResultPropertyValueCollection
    System.Drawing.Design.CategoryNameCollection
    System.Drawing.Design.ToolboxItemCollection
    System.Security.AccessControl.AuthorizationRuleCollection
    System.Web.Management.WebBaseEventCollection
    System.Web.UI.Design.ClientScriptItemCollection
    System.Web.UI.WebControls.WebParts.CatalogPartCollection
    System.Web.UI.WebControls.WebParts.ConnectionInterfaceCollection
    System.Web.UI.WebControls.WebParts.ConsumerConnectionPointCollection
    System.Web.UI.WebControls.WebParts.EditorPartCollection
    System.Web.UI.WebControls.WebParts.ProviderConnectionPointCollection
    System.Web.UI.WebControls.WebParts.TransformerTypeCollection
    System.Web.UI.WebControls.WebParts.WebPartCollection
    System.Web.UI.WebControls.WebParts.WebPartDescriptionCollection
    System.Web.UI.WebControls.WebParts.WebPartVerbCollection
    System.Web.UI.WebControls.WebParts.WebPartZoneCollection
    System.Windows.Forms.FormCollection
    System.Windows.Forms.InputLanguageCollection

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

This implementation does not provide a synchronized (thread safe) wrapper for a ReadOnlyCollectionBase, but derived classes can create their own synchronized versions of the ReadOnlyCollectionBase using the SyncRoot property.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft