日期:2014-05-20  浏览次数:20930 次

继承CollectionBase的类为何不能用索引符?
CollectionBase有继承IList的索引符,为何继承它的类就用不了?
还有IList继承了ICollection和IEnumberable,为何定义CollectionBase时还要写上继承ICollection和IEnumberable?

------解决方案--------------------
需要自己实现,例程:
/// <summary>
/// 获取包含在集合中的 Node 元素。
/// </summary>
/// <param name= "nodeName "> 要查找的 Node 的名称。 </param>
/// <returns> 单个 Role 对象。 </returns>
public Node this[string nodeName]
{
get
{
foreach (Node node in this)
{
if (node.NodeName == nodeName)
{
return node;
}
}

return null;
}
}
------解决方案--------------------
1.是要自己实现

2.还有IList继承了ICollection和IEnumberable,为何定义CollectionBase时还要写上继承ICollection和IEnumberable?

这个问题请楼主参考这个blog
http://www.cnblogs.com/allenlooplee/archive/2004/11/16/64394.html
------解决方案--------------------
1、因为CollectionBase对IList的那个索引器是是显示接口实现……而CollectionBase的用处就是帮你实现IList接口,并帮助你对一些事件如Add注入一些操作,你把它当作IList接口用的时候索引器就会实现,