简体   繁体   English

错误C#:索引超出范围。 必须为非负数且小于集合的大小

[英]Error C#: Index was out of range. Must be non-negative and less than the size of the collection

I've got a problem with creating objects in a for loop and adding them in a list. 我在for循环中创建对象并将它们添加到列表中时遇到了问题。 The program starts with "How much you want to add?" 该程序以“您要添加多少?”开头。 and whatever I write it shows me a message : Index was out of range. 无论我写什么,它都会向我显示一条消息:索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。 What goes wrong? 怎么了? Sorry if there is another similar question but I couldn't find an answer. 抱歉,还有另一个类似的问题,但我找不到答案。 Thank you! 谢谢!

  using System;
using System.Collections.Generic;


class Animals
{

    public int id { get; set; }


    public string name { get; set; }


    public string color { get; set; }     


    public int age { get; set; }

    }

class Program
{
    static void Main(string[] args)
    {
        Console.Write("How much animals you want to add?: ");
        int count = int.Parse(Console.ReadLine());

        var newAnimals = new List<Animals>(count);

        for (int i = 0; i < count; i++)
        {
            newAnimals[i].id = i;

            Console.Write("Write name for animal " + i);
            newAnimals[i].name = Console.ReadLine();

            Console.Write("Write age for animal " + i);
            newAnimals[i].age = int.Parse(Console.ReadLine());

            Console.Write("Write color for animal " + i );
            newAnimals[i].color = Console.ReadLine();

        }

        Console.WriteLine("\nAnimals \tName \tAge \tColor");
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine("\t" + newAnimals[i].name + "\t" + newAnimals[i].age + "\t" + newAnimals[i].color);
        }

        Console.ReadLine();
    }
}

var newAnimals = new List<Animals>(count); creates an empty list with specific Capacity (the total number of elements the internal data structure can hold without resizing) 创建一个具有特定容量的空列表(内部数据结构无需调整大小即可容纳的元素总数)

As it says in MSDN about this constructor: 正如在MSDN中关于此构造函数的说法:

Initializes a new instance of the List class that is empty and has the specified initial capacity. 初始化List类的新实例,该实例为空并具有指定的初始容量。

You need to use Add method to add elements to the list. 您需要使用Add方法将元素添加到列表中。

newAnimals.Add (new Animal() {id = 1, name = "name"});

Since you didn't add any items to your list newAnimals , it is empty, and thus it has no item on index 0. 由于您没有将任何项目添加到列表newAnimals ,因此它为空,因此索引0上没有项目。

Create an instance of Animal and then Add it (I renamed the class name, since it is a singular): 创建Animal的实例,然后AddAdd (我将其重命名为类名,因为它是单数形式):

Animal animal = new Animal();

// do you magic

newAnimals.Add(animal);

After this, you can retrieve the item at index 0. 此后 ,您可以检索索引为0的项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 索引超出范围。 必须为非负数并且小于集合的大小。 C# - Index was out of range. Must be non-negative and less than the size of the collection. C# c#&gt;索引超出范围。 必须为非负数且小于集合的大小 - c# > Index was out of range. Must be non-negative and less than the size of the collection 错误:索引超出范围。 必须是非负的并且小于集合的大小 - Error: Index was out of range. Must be non-negative and less than the size of the collection 错误:System.ArgumentOutOfRangeException:索引超出范围。 必须为非负数且小于集合的大小 - Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection C# WPF 数据网格。 指数超出范围。 必须是非负数且小于集合的大小。 参数“索引” - C# WPF DataGrid. Index was out of range. Must be non-negative and less than the size of the collection. Parameter 'index' 索引超出范围。 必须为非负数并且小于集合错误的大小 - Index was out of range. Must be non-negative and less than the size of the collection error 索引超出范围。 必须为非负数并且小于Nhibernate中的集合错误大小 - Index was out of range. Must be non-negative and less than the size of the collection error in Nhibernate excpetion error:索引超出范围。 必须是非负数且小于集合的大小 - excpetion error :Index was out of range. Must be non-negative and less than the size of the collection 错误索引超出范围。 必须是非负数且小于集合的大小 - Error Index was out of range. Must be non-negative and less than the size of the collection ASP.NET C#:发生错误:索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引(GRIDVIEW) - ASP.NET C#: Error occured: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index (GRIDVIEW)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM