CollectionBase 类

CollectionBase 类

为强类型集合提供抽象(在 Visual Basic 中为 MustInherit)基类。

有关此类型所有成员的列表,请参阅 CollectionBase 成员。

System.Object
   System.Collections.CollectionBase
      派生类

 

[C#]
[Serializable]
public abstract class CollectionBase : IList, ICollection,
   IEnumerable

线程安全

此类型的公共静态(在 Visual Basic 中为 Shared)成员对于多线程操作是安全的。不能保证实例成员是线程安全的。

此实现不为 CollectionBase 提供同步(线程安全)的包装,但派生类可以使用 SyncRoot 属性创建各自的 CollectionBase 同步版本。

通过集合枚举在本质上不是一个线程安全的过程。甚至在对集合进行同步处理时,其他线程仍可以修改该集合,这会导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。

备注

CollectionBase 实例始终是可修改的。有关此类的只读版本,请参见 ReadOnlyCollectionBase。

对实施者的说明:  提供此基类旨在使实施者创建强类型自定义集合变得更容易。实施者应扩展此基类,而不应创建自己的基类。

示例


 

[C#]
using System;
using System.Collections;

public class Int16Collection : CollectionBase  {

   public Int16 this[ int index ]  {
      get  {
         return( (Int16) List[index] );
      }
      set  {
         List[index] = value;
      }
   }

   public int Add( Int16 value )  {
      return( List.Add( value ) );
   }

   public int IndexOf( Int16 value )  {
      return( List.IndexOf( value ) );
   }

   public void Insert( int index, Int16 value )  {
      List.Insert( index, value );
   }

   public void Remove( Int16 value )  {
      List.Remove( value );
   }

   public bool Contains( Int16 value )  {
      // If value is not of type Int16, this will return false.
      return( List.Contains( value ) );
   }

   protected override void OnInsert( int index, Object value )  {
      if ( value.GetType() != Type.GetType("System.Int16") )
         throw new ArgumentException( "value must be of type Int16.", "value" );
   }

   protected override void OnRemove( int index, Object value )  {
      if ( value.GetType() != Type.GetType("System.Int16") )
         throw new ArgumentException( "value must be of type Int16.", "value" );
   }

   protected override void OnSet( int index, Object oldValue, Object newValue )  {
      if ( newValue.GetType() != Type.GetType("System.Int16") )
         throw new ArgumentException( "newValue must be of type Int16.", "newValue" );
   }

   protected override void OnValidate( Object value )  {
      if ( value.GetType() != Type.GetType("System.Int16") )
         throw new ArgumentException( "value must be of type Int16." );
   }

}


public class SamplesCollectionBase  {

   public static void Main()  {
 
      // Creates and initializes a new CollectionBase.
      Int16Collection myI16 = new Int16Collection();

      // Adds elements to the collection.
      myI16.Add( (Int16) 1 );
      myI16.Add( (Int16) 2 );
      myI16.Add( (Int16) 3 );
      myI16.Add( (Int16) 5 );
      myI16.Add( (Int16) 7 );

      // Displays the contents of the collection using the enumerator.
      Console.WriteLine( "Initial contents of the collection:" );
      PrintIndexAndValues( myI16 );

      // Searches the collection with Contains and IndexOf.
      Console.WriteLine( "Contains 3: {0}", myI16.Contains( 3 ) );
      Console.WriteLine( "2 is at index {0}.", myI16.IndexOf( 2 ) );
      Console.WriteLine();

      // Inserts an element into the collection at index 3.
      myI16.Insert( 3, (Int16) 13 );
      Console.WriteLine( "Contents of the collection after inserting at index 3:" );
      PrintIndexAndValues( myI16 );

      // Gets and sets an element using the index.
      myI16[4] = 123;
      Console.WriteLine( "Contents of the collection after setting the element at index 4 to 123:" );
      PrintIndexAndValues( myI16 );

      // Removes an element from the collection.
      myI16.Remove( (Int16) 2 );

      // Displays the contents of the collection using the index.
      Console.WriteLine( "Contents of the collection after removing the element 2:" );
      for ( int i = 0; i < myI16.Count; i++ )  {
         Console.WriteLine( "   [{0}]:   {1}", i, myI16[i] );
      }

   }
 
   public static void PrintIndexAndValues( Int16Collection myCol )  {
      int i = 0;
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   [{0}]:   {1}", i++, myEnumerator.Current );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

Initial contents of the collection:
   [0]:   1
   [1]:   2
   [2]:   3
   [3]:   5
   [4]:   7

Contains 3: True
2 is at index 1.

Contents of the collection after inserting at index 3:
   [0]:   1
   [1]:   2
   [2]:   3
   [3]:   13
   [4]:   5
   [5]:   7

Contents of the collection after setting the element at index 4 to 123:
   [0]:   1
   [1]:   2
   [2]:   3
   [3]:   13
   [4]:   123
   [5]:   7

Contents of the collection after removing the element 2:
   [0]:   1
   [1]:   3
   [2]:   13
   [3]:   123
   [4]:   7

*/

CollectionBase 概述

公共属性
公共属性Count

受 .NET Framework 精简版的支持。

获取包含在 CollectionBase 实例中的元素数。
公共方法
公共方法Clear

受 .NET Framework 精简版的支持。

CollectionBase 实例移除所有对象。
公共方法Equals(从 Object 继承)

受 .NET Framework 精简版的支持。

已重载。确定两个 Object 实例是否相等。
公共方法GetEnumerator

受 .NET Framework 精简版的支持。

返回可循环访问 CollectionBase 实例的枚举数。
公共方法GetHashCode(从 Object 继承)

受 .NET Framework 精简版的支持。

用作特定类型的哈希函数,适合在哈希算法和数据结构(如哈希表)中使用。
公共方法GetType(从 Object 继承)

受 .NET Framework 精简版的支持。

获取当前实例的 Type。
公共方法RemoveAt

受 .NET Framework 精简版的支持。

移除 CollectionBase 实例的指定索引处的元素。
公共方法ToString(从 Object 继承)

受 .NET Framework 精简版的支持。

返回表示当前 Object 的 String。
受保护的构造函数
受保护的构造函数CollectionBase 构造函数

受 .NET Framework 精简版的支持。

初始化 CollectionBase 类的新实例。
受保护的属性
受保护的属性InnerList

受 .NET Framework 精简版的支持。

获取一个 ArrayList,它包含 CollectionBase 实例中元素的列表。
受保护的属性List

受 .NET Framework 精简版的支持。

获取一个 IList,它包含 CollectionBase 实例中元素的列表。
受保护的方法
受保护的方法Finalize(从 Object 继承)

受 .NET Framework 精简版的支持。

已重写。允许 Object 在“垃圾回收”回收 Object 之前尝试释放资源并执行其他清理操作。

在 C# 和 C++ 中,使用析构函数语法来表示终结程序。

受保护的方法MemberwiseClone(从 Object 继承)

受 .NET Framework 精简版的支持。

创建当前 Object 的浅表副本。
受保护的方法OnClear

受 .NET Framework 精简版的支持。

当清除 CollectionBase 实例的内容时执行其他自定义进程。
受保护的方法OnClearComplete

受 .NET Framework 精简版的支持。

在清除 CollectionBase 实例的内容之后执行其他自定义进程。
受保护的方法OnInsert

受 .NET Framework 精简版的支持。

在向 CollectionBase 实例中插入新元素之前执行其他自定义进程。
受保护的方法OnInsertComplete

受 .NET Framework 精简版的支持。

在向 CollectionBase 实例中插入新元素之后执行其他自定义进程。
受保护的方法OnRemove

受 .NET Framework 精简版的支持。

当从 CollectionBase 实例移除元素时执行其他自定义进程。
受保护的方法OnRemoveComplete

受 .NET Framework 精简版的支持。

在从 CollectionBase 实例中移除元素之后执行其他自定义进程。
受保护的方法OnSet

受 .NET Framework 精简版的支持。

当在 CollectionBase 实例中设置值之前执行其他自定义进程。
受保护的方法OnSetComplete

受 .NET Framework 精简版的支持。

当在 CollectionBase 实例中设置值后执行其他自定义进程。
受保护的方法OnValidate

受 .NET Framework 精简版的支持。

当验证值时执行其他自定义进程。
显式接口实现
ICollection.CopyTo从目标数组的指定索引处开始将整个 CollectionBase 复制到兼容的一维 Array。
IList.Add将对象添加到 CollectionBase 的结尾处。
IList.Contains确定 CollectionBase 是否包含特定元素。
IList.IndexOf搜索指定的 Object,并返回整个 CollectionBase 中第一个匹配项的从零开始的索引。
IList.Insert将元素插入 CollectionBase 的指定索引处。
IList.RemoveCollectionBase 中移除特定对象的第一个匹配项。
System.Collections.ICollection.IsSynchronized获取用于指示是否同步 CollectionBase 访问(线程安全)的值。
System.Collections.ICollection.SyncRoot获取可用于同步 CollectionBase 访问的对象。
System.Collections.IList.IsFixedSize获取一个值,该值指示 CollectionBase 是否具有固定大小。
System.Collections.IList.IsReadOnly获取用于指示 CollectionBase 是否为只读的值。
System.Collections.IList.Item获取或设置指定索引处的元素。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值