为泛型集合提供基类。
命名空间: System.Collections.ObjectModel
程序集: mscorlib(在 mscorlib.dll 中)
通过创建 Collection 类的一个构造类型的实例,可以立即使用该类;您只需指定要包含在集合中的对象类型即可。此外,您可以从任意构造类型派生自己的集合类型,或者从 Collection 类本身派生出泛型集合类型。
Collection 类提供了受保护的方法,这些方法可用于在添加和移除项、清除集合或设置现有项的值时自定义该类的行为。
大多数 Collection 对象都可以被修改。但是,不能修改使用只读 IList 对象初始化的 Collection 对象。可使用一个整数索引访问此集合中的元素。此集合中的索引从零开始。
Collection 接受 空引用(在 Visual Basic 中为 Nothing) 作为引用类型的有效值并且允许有重复的元素。
给实现者的说明: 提供此基类旨在使实施者更易于创建自定义集合。实现者最好扩展此基类,而不是创建自己的类。
下面的代码示例演示如何从 Collection 泛型类的构造类型派生集合类,以及如何重写受保护的 InsertItem、RemoveItem、ClearItems 和 SetItem 方法以提供 Add、Insert、Remove 和 Clear 方法以及设置 Item 属性的自定义行为。
此示例提供的自定义行为是 Changed 通知事件,该事件在每个受保护的方法末尾引发。Dinosaurs 类继承 Collection<string>(在 Visual Basic 中为 Collection(Of String)),并定义 Changed 事件,该事件使用 DinosaursChangedEventArgs 类获取事件信息,使用枚举标识更改的类型。
此代码示例调用 Collection 的若干属性和方法以阐释自定义事件。
示例 1
本节包含两个代码示例。第一个示例演示了 Collection 类的若干属性和方法。第二个示例演示如何从 Collection 的构造类型派生集合类,以及如何重写 Collection 的受保护方法以提供自定义行为。

Code
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Demo
{
public static void Main()
{
Collection<string> dinosaurs = new Collection<string>();
dinosaurs.Add("Psitticosaurus");
dinosaurs.Add("Caudipteryx");
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Muttaburrasaurus");
Console.WriteLine("{0} dinosaurs:", dinosaurs.Count);
Display(dinosaurs);
Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
dinosaurs.IndexOf("Muttaburrasaurus"));
Console.WriteLine("\nContains(\"Caudipteryx\"): {0}",
dinosaurs.Contains("Caudipteryx"));
Console.WriteLine("\nInsert(2, \"Nanotyrannus\")");
dinosaurs.Insert(2, "Nanotyrannus");
Display(dinosaurs);
Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);
Console.WriteLine("\ndinosaurs[2] = \"Microraptor\"");
dinosaurs[2] = "Microraptor";
Display(dinosaurs);
Console.WriteLine("\nRemove(\"Microraptor\")");
dinosaurs.Remove("Microraptor");
Display(dinosaurs);
Console.WriteLine("\nRemoveAt(0)");
dinosaurs.RemoveAt(0);
Display(dinosaurs);
Console.WriteLine("\ndinosaurs.Clear()");
dinosaurs.Clear();
Console.WriteLine("Count: {0}", dinosaurs.Count);
}
private static void Display(Collection<string> cs)
{
Console.WriteLine();
foreach( string item in cs )
{
Console.WriteLine(item);
}
}
}
示例 2
下面的代码示例演示了 Collection 的很多属性和方法。此代码示例创建一个字符串集合,使用 Add 方法添加若干个字符串,显示 Count,并列出这些字符串。此示例使用 IndexOf 方法查找字符串的索引,并使用 Contains 方法确定集合中是否存在某一字符串。此示例使用 Insert 方法插入字符串,并使用默认的 Item 属性(C# 中的索引器)检索和设置字符串。此示例使用 Remove 方法按字符串标识移除字符串,并使用 RemoveAt 方法按索引移除字符串。最后,使用 Clear 方法清除集合中的所有字符串。

Code
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Dinosaurs : Collection<string>
{
public event EventHandler<DinosaursChangedEventArgs> Changed;
protected override void InsertItem(int index, string newItem)
{
base.InsertItem(index, newItem);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Added, newItem, null));
}
}
protected override void SetItem(int index, string newItem)
{
string replaced = Items[index];
base.SetItem(index, newItem);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Replaced, replaced, newItem));
}
}
protected override void RemoveItem(int index)
{
string removedItem = Items[index];
base.RemoveItem(index);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Removed, removedItem, null));
}
}
protected override void ClearItems()
{
base.ClearItems();
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Cleared, null, null));
}
}
}
// Event argument for the Changed event.
//
public class DinosaursChangedEventArgs : EventArgs
{
public readonly string ChangedItem;
public readonly ChangeType ChangeType;
public readonly string ReplacedWith;
public DinosaursChangedEventArgs(ChangeType change, string item,
string replacement)
{
ChangeType = change;
ChangedItem = item;
ReplacedWith = replacement;
}
}
public enum ChangeType
{
Added,
Removed,
Replaced,
Cleared
};
public class Demo
{
public static void Main()
{
Dinosaurs dinosaurs = new Dinosaurs();
dinosaurs.Changed += ChangedHandler;
dinosaurs.Add("Psitticosaurus");
dinosaurs.Add("Caudipteryx");
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Muttaburrasaurus");
Display(dinosaurs);
Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
dinosaurs.IndexOf("Muttaburrasaurus"));
Console.WriteLine("\nContains(\"Caudipteryx\"): {0}",
dinosaurs.Contains("Caudipteryx"));
Console.WriteLine("\nInsert(2, \"Nanotyrannus\")");
dinosaurs.Insert(2, "Nanotyrannus");
Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);
Console.WriteLine("\ndinosaurs[2] = \"Microraptor\"");
dinosaurs[2] = "Microraptor";
Console.WriteLine("\nRemove(\"Microraptor\")");
dinosaurs.Remove("Microraptor");
Console.WriteLine("\nRemoveAt(0)");
dinosaurs.RemoveAt(0);
Display(dinosaurs);
}
private static void Display(Collection<string> cs)
{
Console.WriteLine();
foreach( string item in cs )
{
Console.WriteLine(item);
}
}
private static void ChangedHandler(object source,
DinosaursChangedEventArgs e)
{
if (e.ChangeType==ChangeType.Replaced)
{
Console.WriteLine("{0} was replaced with {1}", e.ChangedItem,
e.ReplacedWith);
}
else if(e.ChangeType==ChangeType.Cleared)
{
Console.WriteLine("The dinosaur list was cleared.");
}
else
{
Console.WriteLine("{0} was {1}.", e.ChangedItem, e.ChangeType);
}
}
}