C# System.ArgumentOutOfRange异常索引超出范围。必须为非负数且小于集合的大小。参数名称:索引

C# System.ArgumentOutOfRange异常索引超出范围。必须为非负数且小于集合的大小。参数名称:索引,c#,C#,我想将从数据库接收到的值插入列表 public class Color { public int ColorId {get;set;} public string ColorName {get;set;} public string ShortName {get; set;} } int index = 0; List<Color> color = new List<Color>(); foreach(var items in output) {

我想将从数据库接收到的值插入列表

public class Color
{
   public int ColorId {get;set;}
   public string ColorName {get;set;}
   public string ShortName {get; set;}
}


int index = 0;
List<Color> color = new List<Color>();

foreach(var items in output)
{
   color[index].ColorId = items.ColorId;       
// In this code, it doesn't insert the value of ColorId into color[0] position of list
// color[index].ColorId threw an exception of type 'System.ArgumentOutofRangeException' error
   color[index].ColorName = items.ColorName;
   color[index].ShortName = items.ShortName;
// some other codes 
// .........
index++;       // this is to insert values to list during another (second loop) of foreach statement above

}
公共类颜色
{
公共int ColorId{get;set;}
公共字符串ColorName{get;set;}
公共字符串短名称{get;set;}
}
int指数=0;
列表颜色=新列表();
foreach(输出中的var项)
{
颜色[index].ColorId=items.ColorId;
//在这段代码中,它不会将ColorId的值插入列表的color[0]位置
//color[index]。ColorId引发了类型为“System.ArgumentOutofRangeException”的异常错误
颜色[index].ColorName=items.ColorName;
颜色[索引].ShortName=items.ShortName;
//其他一些代码
// .........
index++;//这是在上述foreach语句的另一个(第二个循环)中插入要列出的值
}
但我得到了这个错误:

System.ArgumentOutOfRange异常索引超出范围。必须是 非负数且小于集合的大小。参数名称: 索引


如何正确插入值?请提供帮助。

当前,您有一个空列表,您正在尝试编辑位置
索引处的值,但该值不存在,因为您的列表为空

您需要添加项目,您可以使用
列表进行添加。add
方法添加到您的列表中

foreach(var item in output)
{
   // Note - I am assuming `item` is a `Color`. If not, you will need
   // to create a new Color with `var color = new Color()`
   color.Add(item);
}

您需要将项目添加到列表中,在此之前不能通过索引引用它们。例如:

foreach(var items in output)
{
    var newColor = new Color
    {
        ColorId = items.ColorId,
        ColorName = items.ColorName,
        shortName = items.ShortName,
    };

    color.Add(newColor);

    // some other codes 
    // .........
    index++;       // this is to insert values to list during another (second loop) of foreach statement above

}
更简单的方法是使用Linq创建列表:

List<Color> color = output
    .Select(o => new Color
    {
        ColorId = o.ColorId,
        ColorName = o.ColorName,
        shortName = o.ShortName,
    })
    .ToList();
列表颜色=输出
.选择(o=>新颜色
{
ColorId=o.ColorId,
ColorName=o.ColorName,
shortName=o.shortName,
})
.ToList();

问题在于,您试图编辑列表对象中尚未添加的行,因此这些行不存在,因此代码失败

foreach(var items in output)
{
   Color newItem = new Color()
   {
      ColorId = items.ColorId,
      ColorName = items.ColorName,
      shortName = items.ShortName,
    };
    color.add(newItem);
    index++;
}
你的
List color=newlist()
初始化颜色列表对象的新实例

但是在您的
foreach
循环中,您使用了一个索引,但它没有成员。这就是错误消息的原因

假设您的
项目
类型为
颜色
,请改用:

color.Add(item);

错误的原因是列表中没有值为
index
的项(即
0
,第一次)。
List colorList=新列表()只创建一个空列表,其中没有任何内容。不能将值指定给不存在的对象

您需要创建一个空白的
Color
对象,然后将其添加到列表中。您不需要
索引
变量

List<Color> colors = new List<Color>();

foreach(var items in output)
{
  Color col = new Color();
  col.ColorId = items.ColorId;       
  col.ColorName = items.ColorName;
  col.ShortName = items.ShortName;
  // .........
  colors.Add(col);
}
List colors=newlist();
foreach(输出中的var项)
{
颜色col=新颜色();
col.ColorId=items.ColorId;
col.ColorName=items.ColorName;
col.ShortName=items.ShortName;
// .........
颜色。添加(col);
}
然后,您也不需要任何类型的二次循环(如代码注释中所述)来将项目添加到列表中——它们已经被添加了


另外,如果
output
已经是
Color
对象的某种列表,那么您可能根本不需要这个循环。您没有提到该变量的类型。

创建
颜色的新对象,为其赋值,最后添加到列表中

List<Color> colorList = new List<Color>();
Color color;
foreach(var item in output)
{
   color = new Color();
   color.ColorId = item.ColorId;       
   color.ColorName = item.ColorName;
   color.ShortName = item.ShortName;
   colorList.Add(color); 
}
List colorList=新列表();
颜色;
foreach(输出中的var项)
{
颜色=新颜色();
color.ColorId=item.ColorId;
color.ColorName=item.ColorName;
color.ShortName=item.ShortName;
颜色列表。添加(颜色);
}

酷如冰

公平地说,我真的很想在这里看到完整的代码(你提到的第二个循环),看看你到底想做什么,因为这看起来也是一个XY问题(这应该是一个新问题,包含了这里提到的修复)