请参见
CollectionBase 成员 | System.Collections 命名空间 | System.Collections.ArrayList | System.Collections.IList | ReadOnlyCollectionBase | 执行不区分区域性的字符串操作 | CollectionBase 成员(Visual J# 语法) | C++ 托管扩展编程
要求
命名空间: System.Collections
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET
程序集: Mscorlib (在 Mscorlib.dll 中)
为强类型集合提供抽象(在 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.Remove | 从 CollectionBase 中移除特定对象的第一个匹配项。 |
System.Collections.ICollection.IsSynchronized | 获取用于指示是否同步 CollectionBase 访问(线程安全)的值。 |
System.Collections.ICollection.SyncRoot | 获取可用于同步 CollectionBase 访问的对象。 |
System.Collections.IList.IsFixedSize | 获取一个值,该值指示 CollectionBase 是否具有固定大小。 |
System.Collections.IList.IsReadOnly | 获取用于指示 CollectionBase 是否为只读的值。 |
System.Collections.IList.Item | 获取或设置指定索引处的元素。 |