c#“索引超出了数组的范围。”

时间:2021-06-09 19:06:09

Trying to solve this problem I am having with using array information in my request.

试图解决这个问题我在我的请求中使用数组信息。

Here is the code I wrote:

这是我写的代码:

public int[] custid = new int[] {};

_request2.CustID = custid[_response.Customers[3].CustID];

Now after debugging the program and walking through each part of the above line, everything is correct, but when I try and run the program it gives me the error "Index was outside the bounds of the array."

现在在调试程序并遍历上面一行的每一部分之后,一切都是正确的,但是当我尝试运行程序时,它给出了错误“索引超出了数组的范围”。

Any ideas on what I am doing wrong?

关于我做错的任何想法?

1 个解决方案

#1


2  

You allocated an empty array. Then you tried to access an element of your empty array.

你分配了一个空数组。然后你试图访问空数组的元素。

You need to have elements in the array like:

您需要在数组中包含以下元素:

public int[] custid = new int[10];

or

public int[] custid = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

If you are looking for something more flexible (like being able to add and remove elements), I suggest a list.

如果您正在寻找更灵活的东西(比如能够添加和删除元素),我建议一个列表。

#1


2  

You allocated an empty array. Then you tried to access an element of your empty array.

你分配了一个空数组。然后你试图访问空数组的元素。

You need to have elements in the array like:

您需要在数组中包含以下元素:

public int[] custid = new int[10];

or

public int[] custid = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

If you are looking for something more flexible (like being able to add and remove elements), I suggest a list.

如果您正在寻找更灵活的东西(比如能够添加和删除元素),我建议一个列表。