1. 定义泛型类
using system;
using system.collections;
using system.collections.generic;
using system.text;
namespace windowsapplication1
{
class blist<t>
{
arraylist arr = new arraylist();
public t this[int i]
{
get
{
return (t)arr[i];
}
set
{
arr.add(value);
}
}
public void add(t p_obj)
{
arr.add(p_obj);
}
public int count
{
get
{
return arr.count;
}
}
}
}
2. 调用(放到任意的窗体事件中)
private void button2_click(object sender, eventargs e)
{
blist<int> _list = new blist<int>(); // <int>里面可以替换任意已知类型
for (int i = 0; i < 10; i++)
{
_list.add( i);
}
for (int i = 0; i < _list.count; i++)
messagebox.show(_list[i].tostring());
}
新闻热点
疑难解答