CollectionBase的使用

    interface IItem
{
object name { get; set; }
object value { get; set; }
void getItemByName();
}

public class ItemClass : IItem
{
#region IItem 成员

public object name
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

public object value
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

public void getItemByName()
{
throw new NotImplementedException();
}

#endregion
}
public class ItemCollection : CollectionBase
{
public void Add(ItemClass item)
{}

public ItemClass this[int i]
{
get
{
return this.InnerList[i] as ItemClass;
}
}

public ItemClass this[string name]
{
get
{
ItemClass reItem
= null;
ArrayList lists
= this.InnerList;
for (int i = 0; i < lists.Count; i++)
{
ItemClass item
= lists[i] as ItemClass;
if (string.Compare(name, item.name.ToString()) == 0)
{
reItem
= item;
}
}
return reItem;
}
}
}

这样就可以通过索引或键值来找到对应的item了。

原文地址:https://www.cnblogs.com/xingbinggong/p/2128959.html