首页 > 解决方案 > 错误 CS1061 - 列表.Item[Int32] 属性

问题描述

这个问题与这个问题这个问题这个问题(相同的错误,不同的源类)和这个问题(相同的错误,不同的原因)非常相似。

在项目中编译我的程序以检查错误后,我收到以下CS1061错误:

Entity.cs(291,24):错误 CS1061:“ List<Entity>”不包含“项目”的定义,并且找不到接受“ List<Entity>”类型的第一个参数的扩展方法“项目”(您是否缺少 using 指令或装配参考?)

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

EntitesArray根据此处的 MSDN 文档,是一个List<Entity>,因此应该具有一个属性。没有名为 的方法或数组。Item[]
EntityItem

课程开始时的声明Entity

public static List<Entity> EntitiesArray;

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

EntitiesArray = new List<Entity>(EntitiesOnScreenCap);

字段EntitiesOnScreenCap等于int200。

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

using System.Collections.Generic;

是什么导致了这个CS1061错误,我该如何解决?

标签: c#compiler-errorsgeneric-list

解决方案


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

EntitiesArray[i]

推荐阅读