ReadOnlyCollectionBase Class
Provides the abstract (MustInherit in Visual Basic) base class for a strongly typed read-only collection.
For a list of all members of this type, see ReadOnlyCollectionBase Members.
System.Object
System.Collections.ReadOnlyCollectionBase
Derived classes
[Visual Basic] <Serializable> MustInherit Public Class ReadOnlyCollectionBase Implements ICollection, IEnumerable [C#] [Serializable] public abstract class ReadOnlyCollectionBase : ICollection, IEnumerable [C++] [Serializable] public __gc __abstract class ReadOnlyCollectionBase : public ICollection, IEnumerable [JScript] public Serializable abstract class ReadOnlyCollectionBase implements ICollection, IEnumerable
Thread Safety
Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. 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 could 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.
Remarks
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 should 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.
Example
[Visual Basic, C#, C++] The following code example implements the ReadOnlyCollectionBase class.
[Visual Basic] Imports System Imports System.Collections Public Class ROCollection Inherits ReadOnlyCollectionBase Public Sub New(sourceList As IList) InnerList.AddRange(sourceList) End Sub 'New Default Public ReadOnly Property Item(index As Integer) As [Object] Get Return InnerList(index) End Get End Property Public Function IndexOf(value As [Object]) As Integer Return InnerList.IndexOf(value) End Function 'IndexOf Public Function Contains(value As [Object]) As Boolean Return InnerList.Contains(value) End Function 'Contains End Class 'ROCollection Public Class SamplesCollectionBase Public Shared Sub Main() ' Create an ArrayList. Dim myAL As 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. Dim myCol As New ROCollection(myAL) ' Display the contents of the collection using the enumerator. Console.WriteLine("Contents of the collection (using enumerator):") 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() ' Display the contents of the collection using the Count property and the Item property. Console.WriteLine("Contents of the collection (using Count and Item):") PrintIndexAndValues2(myCol) End Sub 'Main Public Shared Sub PrintIndexAndValues(myCol As ROCollection) Dim i As Integer = 0 Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator() While myEnumerator.MoveNext() Console.WriteLine(" [{0}]: {1}", i, myEnumerator.Current) i += 1 End While Console.WriteLine() End Sub 'PrintIndexAndValues Public Shared Sub PrintIndexAndValues2(myCol As ROCollection) Dim i As Integer For i = 0 To myCol.Count - 1 Console.WriteLine(" [{0}]: {1}", i, myCol(i)) Next i Console.WriteLine() End Sub 'PrintIndexAndValues2 End Class 'SamplesCollectionBase 'This code produces the following output. ' 'Contents of the collection (using enumerator): ' [0]: red ' [1]: blue ' [2]: yellow ' [3]: green ' [4]: orange ' [5]: purple ' 'Contains yellow: True 'orange is at index 4. ' 'Contents of the collection (using Count and Item): ' [0]: red ' [1]: blue ' [2]: yellow ' [3]: green ' [4]: orange ' [5]: purple [C#] 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 the enumerator. Console.WriteLine( "Contents of the collection (using enumerator):" ); 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(); // Display the contents of the collection using the Count property and the Item property. Console.WriteLine( "Contents of the collection (using Count and Item):" ); PrintIndexAndValues2( myCol ); } public static void PrintIndexAndValues( ROCollection myCol ) { int i = 0; System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.WriteLine( " [{0}]: {1}", i++, myEnumerator.Current ); Console.WriteLine(); } public static void PrintIndexAndValues2( ROCollection myCol ) { for ( int i = 0; i < myCol.Count; i++ ) Console.WriteLine( " [{0}]: {1}", i, myCol[i] ); Console.WriteLine(); } } /* This code produces the following output. Contents of the collection (using enumerator): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple Contains yellow: True orange is at index 4. Contents of the collection (using Count and Item): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple */ [C++] #using <mscorlib.dll> using namespace System; using namespace System::Collections; public __gc class ROCollection : public ReadOnlyCollectionBase { public: ROCollection( IList* sourceList ) { InnerList->AddRange( sourceList ); } public: __property Object* get_Item( int index ) { return( InnerList->Item[index] ); } public: int IndexOf( Object* value ) { return( InnerList->IndexOf( value ) ); } public: bool Contains( Object* value ) { return( InnerList->Contains( value ) ); } }; static void PrintIndexAndValues( ROCollection* myCol ) { int i = 0; System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator(); while ( myEnumerator->MoveNext() ) Console::WriteLine( S" [{0}]: {1}", __box(i++), myEnumerator->Current ); Console::WriteLine(); } static void PrintIndexAndValues2( ROCollection* myCol ) { for ( int i = 0; i < myCol->Count; i++ ) Console::WriteLine( S" [{0}]: {1}", __box(i), myCol->Item[i] ); Console::WriteLine(); } int main() { // Create an ArrayList. ArrayList* myAL = new ArrayList(); myAL->Add( S"red" ); myAL->Add( S"blue" ); myAL->Add( S"yellow" ); myAL->Add( S"green" ); myAL->Add( S"orange" ); myAL->Add( S"purple" ); // Create a new ROCollection that contains the elements in myAL. ROCollection* myCol = new ROCollection( myAL ); // Display the contents of the collection using the enumerator. Console::WriteLine( S"Contents of the collection (using enumerator):" ); PrintIndexAndValues( myCol ); // Search the collection with Contains and IndexOf. Console::WriteLine( S"Contains yellow: {0}", __box(myCol->Contains( S"yellow" ))); Console::WriteLine( S"orange is at index {0}.", __box(myCol->IndexOf( S"orange" ))); Console::WriteLine(); // Display the contents of the collection using the Count property and the Item property. Console::WriteLine( S"Contents of the collection (using Count and Item):" ); PrintIndexAndValues2( myCol ); } /* This code produces the following output. Contents of the collection (using enumerator): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple Contains yellow: True orange is at index 4. Contents of the collection (using Count and Item): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Collections
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: Mscorlib (in Mscorlib.dll)
See Also
ReadOnlyCollectionBase Members | System.Collections Namespace | System.Collections.ArrayList | CollectionBase