ReadOnlyCollectionBase 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供强类型化非泛型只读集合的 abstract 基类。
public ref class ReadOnlyCollectionBase abstract : System::Collections::ICollectionpublic abstract class ReadOnlyCollectionBase : System.Collections.ICollection[System.Serializable]
public abstract class ReadOnlyCollectionBase : System.Collections.ICollection[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class ReadOnlyCollectionBase : System.Collections.ICollectiontype ReadOnlyCollectionBase = class
    interface ICollection
    interface IEnumerable[<System.Serializable>]
type ReadOnlyCollectionBase = class
    interface ICollection
    interface IEnumerable[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ReadOnlyCollectionBase = class
    interface ICollection
    interface IEnumerablePublic MustInherit Class ReadOnlyCollectionBase
Implements ICollection- 继承
- 
				ReadOnlyCollectionBase
- 派生
- 属性
- 实现
示例
下面的代码示例实现 ReadOnlyCollectionBase 类。
using namespace System;
using namespace System::Collections;
public ref class ROCollection: public ReadOnlyCollectionBase
{
public:
   ROCollection( IList^ sourceList )
   {
      InnerList->AddRange( sourceList );
   }
   property Object^ Item [int]
   {
      Object^ get( int index )
      {
         return (InnerList[ index ]);
      }
   }
   int IndexOf( Object^ value )
   {
      return (InnerList->IndexOf( value ));
   }
   bool Contains( Object^ value )
   {
      return (InnerList->Contains( value ));
   }
};
void PrintIndexAndValues( ROCollection^ myCol );
void PrintValues2( ROCollection^ myCol );
int main()
{
   // Create an ArrayList.
   ArrayList^ myAL = gcnew 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 = gcnew ROCollection( myAL );
   // 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.
void PrintIndexAndValues( ROCollection^ myCol )
{
   for ( int i = 0; i < myCol->Count; i++ )
      Console::WriteLine( "   [{0}]:   {1}", i, myCol->Item[ i ] );
   Console::WriteLine();
}
// Uses the enumerator. 
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 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.
*/
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.
*/
Imports System.Collections
Public Class ROCollection
    Inherits ReadOnlyCollectionBase
    Public Sub New(sourceList As IList)
        InnerList.AddRange(sourceList)
    End Sub
    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
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 For Each. This is the preferred method.
        Console.WriteLine("Contents of the collection (using For Each):")
        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()
    End Sub
    ' Uses the Count property and the Item property.
    Public Shared Sub PrintIndexAndValues(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
    ' Uses the For Each statement which hides the complexity of the enumerator.
    ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
    Public Shared Sub PrintValues1(myCol As ROCollection)
        Dim obj As [Object]
        For Each obj In  myCol
            Console.WriteLine("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub
    ' Uses the enumerator. 
    ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
    Public Shared Sub PrintValues2(myCol As ROCollection)
        Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.WriteLine("   {0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class
'This code produces the following output.
'
'Contents of the collection (using For Each):
'   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.
注解
实例 ReadOnlyCollectionBase 始终为只读。 有关此类的可修改版本,请参阅 CollectionBase 。
重要
不建议使用 ReadOnlyCollectionBase 类进行新开发。 建议改用 泛型 ReadOnlyCollection<T> 类。 有关详细信息,请参阅 不应在 GitHub 上使用非泛型集合 。
实施者说明
提供此基类是为了便于实现者创建强类型只读自定义集合。 建议实现者扩展此基类,而不是创建自己的基类。 此基类的成员受到保护,仅通过派生类使用。
此类通过 InnerList 属性提供基础集合,该属性仅供直接派生自 ReadOnlyCollectionBase的类使用。 派生类必须确保其自己的用户无法修改基础集合。
构造函数
| ReadOnlyCollectionBase() | 初始化 ReadOnlyCollectionBase 类的新实例。 | 
属性
| Count | 获取 ReadOnlyCollectionBase 实例中包含的元素数。 | 
| InnerList | 获取 ReadOnlyCollectionBase 实例中包含的元素的列表。 | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetEnumerator() | 返回循环访问 ReadOnlyCollectionBase 实例的枚举器。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| ICollection.CopyTo(Array, Int32) | 从目标数组的指定索引处开始将整个 ReadOnlyCollectionBase 复制到兼容的一维 Array。 | 
| ICollection.IsSynchronized | 获取一个值,该值指示对 ReadOnlyCollectionBase 对象的访问是否同步(线程安全)。 | 
| ICollection.SyncRoot | 获取一个对象,该对象可用于同步对 ReadOnlyCollectionBase 对象的访问。 | 
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 | 
适用于
线程安全性
Visual Basic 中的公共静态 (Shared) 此类型的成员是线程安全的。 但不保证所有实例成员都是线程安全的。
此实现不会为 ReadOnlyCollectionBase提供同步 (线程安全) 包装器,但派生类可以使用 属性创建自己的同步版本的 ReadOnlyCollectionBaseSyncRoot 。
枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。