c# - 错误 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[]
Entity
Item
课程开始时的声明Entity
:
public static List<Entity> EntitiesArray;
方法中的实例化Entity
保证只运行一次:
EntitiesArray = new List<Entity>(EntitiesOnScreenCap);
字段EntitiesOnScreenCap
等于int
200。
这包含在最高范围内(在namespace之前),因此对此不应该有任何问题:
using System.Collections.Generic;
是什么导致了这个CS1061错误,我该如何解决?
解决方案
该Item
属性不是 C# 中的普通属性。这是一种指示您可以使用索引器从 Enumerable 中引用特定项目的方法。要使用它,只需将索引器值放在方括号内:
EntitiesArray[i]
推荐阅读
- python - 将查询和 html 从视图传递到模式弹出窗口
- java - Java Spring EntityManager HTML 自动编码双引号
- javascript - Youtube 搜索 api V3 javascript
- python - 如何在我的代码中运行命令行 python 脚本
- javascript - 为什么我的数组在发布请求后返回未定义?
- reactjs - 有没有办法将功能代码拆分到另一个文件或组件?
- php - 使用令牌访问 Passport 授权路由
- python - 在树莓派 4 上使用 2 个触摸屏
- typescript - 从库中增加类型声明
- dart - Dart 将字符串转换为数组,然后比较两个数组