为什么向数组类型对象声明和赋值会抛出越界错误?
Why declaring and assigning values to array type object is throwing out of bound error?
我定义了这个:
DT_OrderResponseOrderHeaderOperation[] HeaderOperation = new DT_OrderResponseOrderHeaderOperation[] { };
HeaderOperation[0].DAUNO = Convert.ToString(dr["Duration"]);
HeaderOperation[0].DAUNE = Convert.ToString(dr["DurationUnit"]);
但它抛出错误:
Index was outside the bounds of the array.
DT_OrderResponseOrderHeaderOperation
是数组类型。
您需要添加长度说明符来声明数组。长度说明符指示数组包含多少个元素。参见:Single-Dimensional Arrays
DT_OrderResponseOrderHeaderOperation[] HeaderOperation = new DT_OrderResponseOrderHeaderOperation[1];
HeaderOperation[0] = new DT_OrderResponseOrderHeaderOperation()
{
DAUNO = Convert.ToString(dr["Duration"]),
DAUNE = Convert.ToString(dr["DurationUnit"])
};
或者您可以将此 Array Initialization 用作 follow.In 这种情况下,不需要长度说明符,因为它已经由初始化列表中的元素数提供。
DT_OrderResponseOrderHeaderOperation[] HeaderOperation = new DT_OrderResponseOrderHeaderOperation[]
{
new DT_OrderResponseOrderHeaderOperation()
{
DAUNO = Convert.ToString(dr["Duration"]),
DAUNE = Convert.ToString(dr["DurationUnit"])
}
};
我定义了这个:
DT_OrderResponseOrderHeaderOperation[] HeaderOperation = new DT_OrderResponseOrderHeaderOperation[] { };
HeaderOperation[0].DAUNO = Convert.ToString(dr["Duration"]);
HeaderOperation[0].DAUNE = Convert.ToString(dr["DurationUnit"]);
但它抛出错误:
Index was outside the bounds of the array.
DT_OrderResponseOrderHeaderOperation
是数组类型。
您需要添加长度说明符来声明数组。长度说明符指示数组包含多少个元素。参见:Single-Dimensional Arrays
DT_OrderResponseOrderHeaderOperation[] HeaderOperation = new DT_OrderResponseOrderHeaderOperation[1];
HeaderOperation[0] = new DT_OrderResponseOrderHeaderOperation()
{
DAUNO = Convert.ToString(dr["Duration"]),
DAUNE = Convert.ToString(dr["DurationUnit"])
};
或者您可以将此 Array Initialization 用作 follow.In 这种情况下,不需要长度说明符,因为它已经由初始化列表中的元素数提供。
DT_OrderResponseOrderHeaderOperation[] HeaderOperation = new DT_OrderResponseOrderHeaderOperation[]
{
new DT_OrderResponseOrderHeaderOperation()
{
DAUNO = Convert.ToString(dr["Duration"]),
DAUNE = Convert.ToString(dr["DurationUnit"])
}
};