CollectionBase 类
为强类型集合提供 abstract 基类。
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class CollectionBase : IList, ICollection, IEnumerable
CollectionBase 实例始终是可修改的。有关此类的只读版本,请参见 ReadOnlyCollectionBase。
CollectionBase 的容量是 CollectionBase 可以保存的元素数。CollectionBase 的默认初始容量为 16。向 CollectionBase 添加元素时,将通过重新分配来根据需要自动增大容量。通过显式设置 Capacity 属性可以减小容量。
给实现者的说明 提供此基类旨在使实施者创建强类型自定义集合变得更容易。实现者最好扩展此基类,而不是创建自己的类。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Test_TestInher : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Main();
}
}
public void Main()
{
// Create and initialize a new CollectionBase.
Int16Collection myI16 = new Int16Collection();
// Add elements to the collection.
myI16.Add((Int16)1);
myI16.Add((Int16)2);
myI16.Add((Int16)3);
myI16.Add((Int16)5);
myI16.Add((Int16)7);
// Display the contents of the collection using foreach. This is the preferred method.
Response.Write("Contents of the collection (using foreach):");
PrintValues1(myI16);
// Display the contents of the collection using the enumerator.
Response.Write("Contents of the collection (using enumerator):");
PrintValues2(myI16);
// Display the contents of the collection using the Count property and the Item property.
Response.Write("Initial contents of the collection (using Count and Item):");
PrintIndexAndValues(myI16);
// Search the collection with Contains and IndexOf.
Response.Write(string.Format("Contains 3: {0}", myI16.Contains(3)));
Response.Write(string.Format("2 is at index {0}.", myI16.IndexOf(2)));
// Response.Write();
// Insert an element into the collection at index 3.
myI16.Insert(3, (Int16)13);
Response.Write("Contents of the collection after inserting at index 3:");
PrintIndexAndValues(myI16);
// Get and set an element using the index.
myI16[4] = 123;//c.******** c[int]
Response.Write("Contents of the collection after setting the element at index 4 to 123:");
PrintIndexAndValues(myI16);
// Remove an element from the collection.
myI16.Remove((Int16)2);
// Display the contents of the collection using the Count property and the Item property.
Response.Write("Contents of the collection after removing the element 2:");
PrintIndexAndValues(myI16);
}
// Uses the Count property and the Item property.
public void PrintIndexAndValues(Int16Collection myCol)
{
for (int i = 0; i < myCol.Count; i++)
Response.Write(string.Format(" [{0}]: {1}", i, myCol[i]));
// Response.Write();
}
// 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 void PrintValues1(Int16Collection myCol)
{
foreach (Int16 i16 in myCol)
Response.Write(string.Format(" {0}", i16));
// Response.End();
}
// Uses the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public void PrintValues2(Int16Collection myCol)
{
System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
while (myEnumerator.MoveNext())
Response.Write(string.Format(" {0}", myEnumerator.Current));
// Response.End();
}
}
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)
{
// Insert additional code to be run only when inserting values.
}
protected override void OnRemove(int index, Object value)
{
// Insert additional code to be run only when removing values.
}
protected override void OnSet(int index, Object oldValue, Object newValue)
{
// Insert additional code to be run only when setting values.
}
protected override void OnValidate(Object value)
{
if (value.GetType() != typeof(System.Int16))
throw new ArgumentException("value must be of type Int16.", "value");
}
}

