错误CS1061-List <t> .Item [Int32]属性

时间:2018-07-07 04:09:42

标签: c# compiler-errors generic-list

此问题与this questionthis questionthis question(相同的错误,不同的源类)和this question(相同的错误,不同的原因)非常相似。 / p>

在项目中编译程序以检查错误后,出现以下 CS1061 错误:

  

Entity.cs(291,24):错误CS1061:“ List<Entity>”不包含   “项目”的定义,并且没有扩展方法“项目”接受第一个   可以找到类型'List<Entity>的参数(您是否缺少   使用指令还是程序集引用?)

Entity.cs 是发生错误的文件的名称,该错误发生在 Load()函数中,如下所示:

public void Load(){
    /*
        Try to spawn the entity.
        If there are too many entities
        on-screen, unload any special
        effect entities.
        If that fails, produce an error message
    */
    try{
        if(EntitiesArray.Count >= EntitiesOnScreenCap){
            if(this.Type == EntityType.SpecialEffect)
                Unload();
            else
                throw null;
        }else
            //Place the entity in the first available slot in the array
            for(int i = 0; i < EntitiesArray.Capacity; i++)
                if(EntitiesArray.Item[i] == NullEntity)
                    EntitiesArray.Item[i] = this;
    }catch(Exception exc){
        throw new LoadException("Failed to load entity.");
    }
}

错误发生在这些行(第291和292行):

if(EntitiesArray.Item[i] == NullEntity)
    EntitiesArray.Item[i] = this;

NullEntity是类型Entity的字段,并且this也是类型Entity的字段。

EntitesArrayList<Entity>,因此,根据MSDN文档here,应具有Item[]属性。
Entity没有名为Item的方法或数组。

Entity课程开始时的声明:

public static List<Entity> EntitiesArray;

Entity中保证只能运行一次的方法中的实例化:

EntitiesArray = new List<Entity>(EntitiesOnScreenCap);

字段EntitiesOnScreenCap是等于200的int

这是包含在最高范围内的(在 namespace 之前),因此对此应该没有任何问题:

using System.Collections.Generic;

是什么原因导致此 CS1061 错误,该如何解决?

1 个答案:

答案 0 :(得分:2)

Item属性在C#中不是常规属性。这是一种指示可以使用索引器引用Enumerable中特定项目的方法。要使用它,只需将索引器值放在方括号内:

EntitiesArray[i]
相关问题