一个具备完整能力的Collection类必须实现IList接口,此接口提供了修改Collection内元素的能力.
IList 是 ICollection 接口的子代,并且是所有非泛型列表的基接口。IList 实现有三种类别:只读、固定大小和可变大小。无法修改只读IList。固定大小的IList 不允许添加或移除元素,但允许修改现有元素。可变大小的IList 允许添加、移除和修改元素。
下面是此接口的原型。
// 摘要:
// 表示可按照索引单独访问的对象的非泛型集合。
[ComVisible(true)]
public interface IList : ICollection, IEnumerable
{
// 摘要:
// 获取一个值,该值指示 System.Collections.IList 是否具有固定大小。
//
// 返回结果:
// 如果 System.Collections.IList 具有固定大小,则为 true;否则为 false。
bool IsFixedSize { get; }
//
// 摘要:
// 获取一个值,该值指示 System.Collections.IList 是否为只读。
//
// 返回结果:
// 如果 System.Collections.IList 为只读,则为 true;否则为 false。
bool IsReadOnly { get; }
// 摘要:
// 获取或设置指定索引处的元素。
//
// 参数:
// index:
// 要获得或设置的元素从零开始的索引。
//
// 返回结果:
// 指定索引处的元素。
//
// 异常:
// System.ArgumentOutOfRangeException:
// index 不是 System.Collections.IList 中的有效索引。
//
// System.NotSupportedException:
// 设置该属性,而且 System.Collections.IList 为只读。
object this[int index] { get; set; }
// 摘要:
// 向 System.Collections.IList 中添加项。
//
// 参数:
// value:
// 要添加到 System.Collections.IList 的对象。
//
// 返回结果:
// 新元素所插入到的位置,或为 -1 以指示未将该项插入到集合中。
//
// 异常:
// System.NotSupportedException:
// System.Collections.IList 是只读的。- 或 -System.Collections.IList 具有固定大小。
int Add(object value);
//
// 摘要:
// 从 System.Collections.IList 中移除所有项。
//
// 异常:
// System.NotSupportedException:
// System.Collections.IList 是只读的。
void Clear();
//
// 摘要:
// 确定 System.Collections.IList 是否包含特定值。
//
// 参数:
// value:
// 要在 System.Collections.IList 中查找的对象。
//
// 返回结果:
// 如果在 System.Collections.IList 中找到 System.Object,则为 true;否则为 false。
bool Contains(object value);
//
// 摘要:
// 确定 System.Collections.IList 中特定项的索引。
//
// 参数:
// value:
// 要在 System.Collections.IList 中查找的对象。
//
// 返回结果:
// 如果在列表中找到 value,则为该项的索引;否则为 -1。
int IndexOf(object value);
//
// 摘要:
// 在 System.Collections.IList 中的指定索引处插入项。
//
// 参数:
// index:
// 从零开始的索引,应在该位置插入 value。
//
// value:
// 要插入到 System.Collections.IList 中的对象。
//
// 异常:
// System.ArgumentOutOfRangeException:
// index 不是 System.Collections.IList 中的有效索引。
//
// System.NotSupportedException:
// System.Collections.IList 是只读的。- 或 -System.Collections.IList 具有固定大小。
//
// System.NullReferenceException:
// value 在 System.Collections.IList 中是 null 引用。
void Insert(int index, object value);
//
// 摘要:
// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
//
// 参数:
// value:
// 要从 System.Collections.IList 中移除的对象。
//
// 异常:
// System.NotSupportedException:
// System.Collections.IList 是只读的。- 或 -System.Collections.IList 具有固定大小。
void Remove(object value);
//
// 摘要:
// 移除指定索引处的 System.Collections.IList 项。
//
// 参数:
// index:
// 从零开始的索引(属于要移除的项)。
//
// 异常:
// System.ArgumentOutOfRangeException:
// index 不是 System.Collections.IList 中的有效索引。
//
// System.NotSupportedException:
// System.Collections.IList 是只读的。- 或 -System.Collections.IList 具有固定大小。
void RemoveAt(int index);
}
由原型的预定义中可以知道,事实上IList接口是ICollection及IEnumerable两个接口的综合体,并额外提供了存取Collection内元素的能力,下面程序时简单的实现例子:
public class MyList:IList
{
//其实这里定义了一个ArrayList类型的字段
private ArrayList list;
public MyList()
{
//初始化列表
list = new ArrayList();
}
#region IList Members
//其实就是MyList对象的一个属性
public bool IsReadOnly
{
get
{
return false;
}
}
//通过索引获取元素的值
public object this[int index]
{
get
{
return list[index];
}
set
{
list[index] =value;
}
}
//移除元素的值
public void RemoveAt(int index)
{
list.Remove(index);
}
public void Insert(int index,object value)
{
list.Inset(index,value);
}
public bool Contains(object value)
{
return list.Contains(value)
}
public int IndexOf(object value)
{
return list.IndexOf(value);
}
public int Add(object value)
{
list.Add(value)
}
public void Clear()
{
list.Clear();
}
public bool IsFixedSite
{
get
{
return false;
}
}
#endregion
#region ICollection Members
public bool IsSynchronized
{
get{ return true; }
}
public int Count
{
get
{
return list.Length;
}
}
public void CopyTo(Array array,int index)
{
list.CopyTo(array,index);
}
public object SyncRoot
{
get { return root; }
}
#endregioin
#region IEnumerable Members
public IEnumerable GetEnumerator()
{
return list.GetEnumerator();
}
#endregion
}