1

This problem is very similar to this question, this question, this question (same error, different source class), and this question (same error, different reason).

After compiling my programs in a project to check for errors, I got the following CS1061 error:

Entity.cs(291,24): error CS1061: 'List<Entity>' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'List<Entity>' could be found (are you missing a using directive or an assembly reference?)

Entity.cs is the name of the file where the error occurred, and said error occurs in the Load() function, shown below:

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.");
    }
}

The error happens on these lines (lines 291 & 292):

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

NullEntity is a field of type Entity, and this is also of type Entity.

EntitesArray is a List<Entity> and, therefore, should have an Item[] property, according to the MSDN documentation here.
Entity does not have a method or array named Item.

Declaration at beginning of class Entity:

public static List<Entity> EntitiesArray;

Instantiation in a method in Entity that is guaranteed to run only once:

EntitiesArray = new List<Entity>(EntitiesOnScreenCap);

Field EntitiesOnScreenCap is an int equal to 200.

This is included at the highest scope (before the namespace), so there should not be any problems regarding this:

using System.Collections.Generic;

What is causing this CS1061 error, and how can I fix it?

absoluteAquarian
  • 510
  • 3
  • 12

1 Answers1

2

The Item property isn't a normal property in C#. It is a way to indicate that you can use an indexer to reference a specific item from the Enumerable. To use it, you just put the indexer value inside square brackets:

EntitiesArray[i]
Elezar
  • 1,467
  • 1
  • 15
  • 22
  • Oh, so it's like using a regular array in that respect. – absoluteAquarian Jul 07 '18 at 04:19
  • The syntax is the same, and in the case of a List, yes, it basically works the same way. However, it's possible to have non-integer indexers, and they can even be overloaded. For example, a `NameValueCollection` has both an int and a string indexer. So you can reference an item either by position or name. For example, both `nvc[0]` and `nvc["FirstName"]` are valid for a `NameValueCollection` named `nvc`. – Elezar Jul 07 '18 at 04:40