【发布时间】:2011-06-22 06:50:25
【问题描述】:
您好,我在这里遇到语法问题,完全不知道如何正确执行此操作。
我的自定义列表来源:(有错误)GetEnmerator() 和大量警告
错误和警告是:
错误 1“EntityListEnumerator”未实现接口成员“System.Collections.IEnumerator.Current”。 “EntityListEnumerator.Current”无法实现“System.Collections.IEnumerator.Current”,因为它没有“object”的匹配返回类型。
警告 2 'EntityList.Add(T)' 隐藏继承的成员 'System.Collections.Generic.List.Add(T)'。如果要隐藏,请使用 new 关键字。
警告 3 'EntityList.this[int]' 隐藏了继承的成员 'System.Collections.Generic.List.this[int]'。如果要隐藏,请使用 new 关键字。
警告 4 'EntityList.Remove(T)' 隐藏继承的成员 'System.Collections.Generic.List.Remove(T)'。如果要隐藏,请使用 new 关键字。
警告 5 'EntityList.IndexOf(T)' 隐藏继承的成员 'System.Collections.Generic.List.IndexOf(T)'。如果要隐藏,请使用 new 关键字。
警告 6 'EntityList.Contains(T)' 隐藏继承的成员 'System.Collections.Generic.List.Contains(T)'。如果要隐藏,请使用 new 关键字。
警告 7 'EntityList.Count' 隐藏了继承的成员 'System.Collections.Generic.List.Count'。如果要隐藏,请使用 new 关键字。
class EntityList<T> : List<T>, IEnumerable<T> where T : Entity
{
private const int DEFAULT_CAPACITY = 1600, MIN_VALUE = 1;
public T[] entities;
public HashSet<int> indicies = new HashSet<int>();
public int curIndex = MIN_VALUE;
public int capacity;
public EntityList(int capacity) {
entities = new T[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 this[int index]
{
get
{
return entities[index];
}
set
{
entities[index] = value;
}
}
public void Remove(T entity)
{
entities[entity.getIndex()] = null;
indicies.Remove(entity.getIndex());
decreaseIndex();
}
public Entity Remove(int index)
{
Object temp = entities[index];
entities[index] = null;
indicies.Remove(index);
decreaseIndex();
return (Entity)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();
}
}
}
这是我的 EntityListEnumerator 源,到处都有错误,主要是一些与我的真实 Type 到 Generic T 类型的拳击对话
class EntityListEnumerator<T> : IEnumerator<T> where T : Entity
{
private int[] indicies;
private object[] entities;
private EntityList<T> entityList;
protected int curIndex; //current index
protected T _current; //current enumerated object in the collection
public EntityListEnumerator(object[] entities, HashSet<int> indicies, EntityList<T> entityList)
{
this.entities = entities;
this.indicies = indicies.ToArray();
this.entityList = entityList;
curIndex = -1;
}
public virtual T 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(T); //reset current object
curIndex = -1;
}
// Dispose method
public virtual void Dispose()
{
entityList = null;
_current = default(T);
curIndex = -1;
}
}
如何修复该错误并正确消除这些警告。谢谢
【问题讨论】:
标签: visual-studio list generics collections ienumerable