如何制作自己的容器类?

Bluclyf 2003-10-30 05:08:53
我的程序需要一个串口通讯类。一个串口同时和多个相连的设备通讯,我想让这个类成为一个容器,其他设备类可以添加如其中。
...全文
42 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaneboy 2003-10-31
  • 打赏
  • 举报
回复
.net提供了三个抽象基类让我们继承,以实现自己的集合类:

CollectionBase、ReadOnlyCollectionBase、DictionaryBase

在MSDN帮助里面讲解它们的时候又一个自定义int类型集合类的例子


下面的链接是一篇不错的文章,在C#中实现集合类:
http://dev.luca.minudel.it/dotnet/implementcollections/tutorial_eng.htm
wolve 2003-10-31
  • 打赏
  • 举报
回复
一般是这样,你的类得实现容器的基本接口,如Ilist,ICollection等,根据需要实现吧。然后类的内部实现就是在已有容器类(arraylist,hastable等)上进行进一步封装
Tadpole0510 2003-10-31
  • 打赏
  • 举报
回复
不会,帮忙顶一下
Alton1981 2003-10-31
  • 打赏
  • 举报
回复
using System;
using System.Collections;

class MyClass
{
protected ArrayList data=new ArrayList();

public object this [int idx]
{
get
{
if(idx>-1 && idx<data.Count)
{
return data[idx];
}
else
{
return null;
}
}
set
{
if(idx>-1 && idx<data.Count)
{
data[idx]=value;
}
else if(idx==data.Count)
{
data.Add(value);
}
else
{
// throw an Exception
}
}
}
}

class IndexApp
{
public static void main()
{
MyClass mc=new MyClass();
mc[0]="one";
mc[1]="two";
mc[2]="three";
Concole.Writeln("{0} {1} {2}",mc[0],mc[1],mc[2]);
}
}
Bluclyf 2003-10-31
  • 打赏
  • 举报
回复
怎么用??
Alton1981 2003-10-30
  • 打赏
  • 举报
回复
用索引器。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧