求助C#作业

来源:百度知道 编辑:UC知道 时间:2024/06/06 00:24:05
编写一个集合类
1、是否实现IList接口自行决定
2、如果实现IList接口,则接口功能自行编写,不得继承现有类如ArrayList
3、长度可以自动生长和收缩

1楼说来简单,你貌似很牛逼嘛,你写个实现Ilist试试我看看,
说实话下面这个是微软的ArrayList类实现文件,我用反编译查看了下发现直接改名给你有些不负责任,因为微软的ArrayList同时实现了集合IConllection,许多访问是为了保存线程安全做的,感觉没必要都贴出来就删除了一部分无关的代码,留下的仅仅是个可以用实现foreach必须接口IEnumerable和IList的插入删除等等自扩充功能,这个目前还不是泛型,你可以发现内容依然是个object数组,因此这里当你用索引器访问时存在装箱的可能,如果你想实现List<T>泛型模式,那么这个内部数组也要支持泛型,不过好处是速度 快了,呵呵。
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Threading;
public class WorkList : IList, IEnumerable
{
private const int _defaultCapacity = 4;
private object[] _items;
private int _size;
private static readonly object[] emptyArray = new object[0];

public WorkList()
{
this._items = emptyArray;
}

public WorkList(int capacity)
{
if (capacity <