List C#尝试使用静态索引计数器创建自己的列表

List C#尝试使用静态索引计数器创建自己的列表,list,ienumerable,icollection,enumerator,List,Ienumerable,Icollection,Enumerator,我想制作自己的列表,它能够在foreach循环中工作。除了有其他重要的命令外,List还有IndexOf(这是我唯一不喜欢List的地方,因为它是动态变化的)。我列表中的那个必须跟踪所有索引。除了添加,包含,计数,删除,以及[]访问器(不知道如何做),所以现在获取应该可以做到这一点 列表应该被铸造到一个名为Entity的基类中,该基类因其相似性而在另外两个类Npc/Player中共享 无论如何,我无法控制我正在为这个服务器编码的客户机,但协议规定要求所有玩家跟踪索引,而不必对常规列表的索引进行任

我想制作自己的
列表
,它能够在
foreach循环中工作
。除了有其他重要的命令外,
List
还有IndexOf(这是我唯一不喜欢List的地方,因为它是动态变化的)。我列表中的那个必须跟踪所有索引。除了
添加
包含
计数
删除
,以及
[]
访问器(不知道如何做),所以现在
获取
应该可以做到这一点

列表应该被铸造到一个名为Entity的基类中,该基类因其相似性而在另外两个类Npc/Player中共享

无论如何,我无法控制我正在为这个服务器编码的客户机,但协议规定要求所有玩家跟踪索引,而不必对常规列表的索引进行任何剧烈的动态更改

我一直在学习如何制作自己的
收藏
我发现了3个我无法解决的错误

Argument 3: cannot convert from 'EntityList<T>' to 'EntityList<Entity>'
参数3:无法从“EntityList”转换为“EntityList”

Cannot apply indexing with [] to an expression of type 'EntityList<Entity>'
The best overloaded method match for 'EntityListEnumerator<T>.EntityListEnumerator(object[], System.Collections.Generic.HashSet<int>, EntityList<Entity>)' has some invalid arguments
无法将带[]的索引应用于“EntityList”类型的表达式

Cannot apply indexing with [] to an expression of type 'EntityList<Entity>'
The best overloaded method match for 'EntityListEnumerator<T>.EntityListEnumerator(object[], System.Collections.Generic.HashSet<int>, EntityList<Entity>)' has some invalid arguments
与“EntityListNumerator.EntityListNumerator(object[],System.Collections.Generic.HashSet,EntityList)”匹配的最佳重载方法具有一些无效参数
我也想问一下,我做得对吗?或者有些事情看起来很诡诈

谢谢你的帮助我很感激。。C#的这一部分似乎非常先进,很难理解

源到EntityList

public class EntityList<T> : ICollection<T> where T : Entity
{
        private const int DEFAULT_CAPACITY = 1600, MIN_VALUE = 1;
        public object[] entities;
        public HashSet<int> indicies = new HashSet<int>();
        public int curIndex = MIN_VALUE;
        public int capacity;

    public EntityList(int capacity) {
        entities = new object[capacity];
        this.capacity = capacity;
    }

    public EntityList() 
    : this(DEFAULT_CAPACITY) {}

public bool Add(T entity)
{
    Add(entity, curIndex);
    return true;
}

    public void Add(T entity, int index) {
        if (entities[curIndex] != null) {
            increaseIndex();
            Add(entity, curIndex);
        } else {
            entities[curIndex] = entity;
            entity.setIndex(index);
            indicies.Add(curIndex);
            increaseIndex();
        }
    }

public T Get(int index)
{
    return (T)entities[index];
}

public void Remove(T entity)
{
    entities[entity.getIndex()] = null;
    indicies.Remove(entity.getIndex());
    decreaseIndex();
}

public T Remove(int index)
{
    Object temp = entities[index];
    entities[index] = null;
    indicies.Remove(index);
    decreaseIndex();
    return (T)temp;
}

IEnumerator IEnumerable.GetEnumerator()
{
    return new EntityListEnumerator<T>(entities, indicies, this);
}

    private void increaseIndex() {
        curIndex++;
        if (curIndex >= capacity) {
            curIndex = MIN_VALUE;
        }
    }

private void decreaseIndex()
{
        curIndex--;
        if (curIndex <= capacity)
            curIndex = MIN_VALUE;
    }

    public int IndexOf(T entity) {
        foreach(int index in indicies) {
            if (entities[index].Equals(entity)) {
                return index;
            }
        }
        return -1;
    }

public bool Contains(T entity)
{
    return IndexOf(entity) > -1;
}

    public int Count {
    get
    {
        return indicies.Count();
    }
    }
}
公共类EntityList:ICollection,其中T:Entity
{
专用常量int默认容量=1600,最小值=1;
公共对象[]实体;
public HashSet indicies=新HashSet();
public int curIndex=最小值;
公共能力;
公共实体列表(内部容量){
实体=新对象[容量];
这个。容量=容量;
}
公共实体列表()
:此(默认容量){}
公共布尔添加(T实体)
{
添加(实体,curIndex);
返回true;
}
公共空添加(T实体,整数索引){
if(实体[curIndex]!=null){
增加索引();
添加(实体,curIndex);
}否则{
实体[curIndex]=实体;
实体.setIndex(index);
标记。添加(curIndex);
增加索引();
}
}
公共T获取(整数索引)
{
返回(T)实体[指数];
}
公共无效删除(T实体)
{
实体[entity.getIndex()]=null;
删除(entity.getIndex());
递减指数();
}
公共T删除(整型索引)
{
对象温度=实体[索引];
实体[索引]=空;
标记。删除(索引);
递减指数();
返回(T)温度;
}
IEnumerator IEnumerable.GetEnumerator()
{
返回新的EntityListNumerator(实体、标记、此);
}
私有无效增量索引(){
curIndex++;
如果(curIndex>=容量){
curIndex=最小值;
}
}
私有void decreaseIndex()
{
库林德斯--;
if(curIndex-1;
}
公共整数计数{
得到
{
返回标记。Count();
}
}
}
源到EntityListNumerator

class EntityListEnumerator<E> : IEnumerator<E> where E : Entity
{
    private int[] indicies;
        private object[] entities;
        private EntityList<Entity> entityList;

protected int curIndex; //current index
protected E _current; //current enumerated object in the collection

public EntityListEnumerator(object[] entities, HashSet<int> indicies, EntityList<Entity> entityList)
{
        this.entities = entities;
        this.indicies = indicies.ToArray();
        this.entityList = entityList;
    curIndex = -1;
    }

public virtual E Current
{
    get
    {
        return _current;
    }
}

public virtual bool MoveNext()
{
    //make sure we are within the bounds of the collection
    if (++curIndex >= entityList.Count)
    {
        //if not return false
        return false;
    }
    else
    {
        //if we are, then set the current element
        //to the next object in the collection
        _current = entityList[indicies[curIndex]];
    }
    //return true
    return true;
}

    public void Remove() {
    if (curIndex >= 1)
    {
        entityList.Remove(indicies[curIndex - 1]);
        }
    }

// Reset the enumerator
public virtual void Reset()
{
    _current = default(E); //reset current object
    curIndex = -1;
}

// Dispose method
public virtual void Dispose()
{
    entityList = null;
    _current = default(E);
    curIndex = -1;
}
class EntityListEnumerator:IEnumerator其中E:Entity
{
私有int[]标识;
私有对象[]实体;
私有实体列表;
受保护的int curIndex;//当前索引
受保护的E _current;//集合中当前枚举的对象
公共EntityListNumerator(对象[]实体、哈希集标记、EntityList EntityList)
{
这个。实体=实体;
this.indicies=indicies.ToArray();
this.entityList=entityList;
curIndex=-1;
}
公共虚拟电流
{
得到
{
返回电流;
}
}
公共虚拟bool MoveNext()
{
//确保我们在收集范围内
如果(++curIndex>=entityList.Count)
{
//如果不返回false
返回false;
}
其他的
{
//如果是,则设置当前元素
//指向集合中的下一个对象
_当前=实体列表[标识[curIndex];
}
//返回真值
返回true;
}
公共空间删除(){
如果(curIndex>=1)
{
删除(标记[curIndex-1]);
}
}
//重置枚举数
公共虚拟无效重置()
{
_当前=默认值(E);//重置当前对象
curIndex=-1;
}
//处置方法
公共虚拟void Dispose()
{
entityList=null;
_当前=默认值(E);
curIndex=-1;
}

}

我不能100%确定您需要什么,但是您是否查看了Dictionary对象?如果使用通用版本,您可以定义自己的密钥并使其包含您需要的任何项

请阅读您最后的评论:
您可以决定不使用列表的索引作为位置,但可以使用我提到的字典,或者只使用列表,而不是使用索引器(将“出错”的数字)检查列表中项目的属性(让列表中的项目保留自己的“索引”,由您100%控制)

很抱歉问,为什么?为什么?为什么?为什么我要尝试这样做?从长远来看,它会更灵活,我认为这就像把引擎藏在引擎盖下,因为你知道它会正常工作,你不必太担心它。再加上它的代码重用,因为没有这个类,我会有两套indeNpc和玩家都有x个守护者。我不知道为什么,但我想解决这个问题。谢谢。如果你知道有什么问题,请告诉我。我是一个新手,正在学习如何使用C#如果你的意思是,我为什么不使用列表?列表的索引是动态的。我希望它们是静态的。这就是总结。假设第500个索引中的一个玩家注销。所有玩家在500 wi之后如果第一个玩家注销的话,我会被转移,并把索引的顺序搞砸。。