![](/img/trans.png)
[英]System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
[英]System.ArgumentOutOfRangeException Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
我想将从数据库接收到的值插入到列表中。
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
}
但我得到这个错误:
System.ArgumentOutOfRangeException 索引超出范围。 必须是非负数且小于集合的大小。 参数名称:索引
如何正确插入值? 请帮忙。
目前,您有一个空列表,并且您正在尝试编辑 position index
处的值 - 但该值不存在,因为您的列表为空。
您需要添加项目,您可以使用List.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();
问题是您正在尝试编辑列表 object 中尚未添加的行,因此不存在,因此代码失败。
foreach(var items in output)
{
Color newItem = new Color()
{
ColorId = items.ColorId,
ColorName = items.ColorName,
shortName = items.ShortName,
};
color.add(newItem);
index++;
}
你的List<Color> color = new List<Color>();
初始化颜色列表 object 的新实例。
但是在您的foreach
循环中,您正在使用它的索引,但它没有成员。 这就是错误消息的原因。
假设您的items
是Color
类型,请改用:
color.Add(item);
错误的原因是列表中没有index
值处的项目(即0
,第一次)。 List<Color> colorList = new List<Color>();
只是创建一个空列表,其中没有任何内容。 您不能将值分配给不存在的 object。
您需要创建一个空白Color
object,然后将其添加到列表中。 您不需要index
变量。
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);
}
然后,您也不需要任何类型的辅助循环(如您的代码注释中所述)将项目添加到列表中 - 它们已经添加。
PS 如果output
已经是某种Color
对象列表,那么您可能根本不需要这个循环。 您没有提到该变量的类型是什么。
新建color
的 object ,为其赋值,最后添加到列表中
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);
}
酷如冰
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.