数组包含9个元素,但超出范围

本文关键字:范围 包含 9个 元素 数组 | 更新日期: 2023-09-27 18:14:33

我得到这个错误,即使我的代码设置为处理适当数量的参数。c#使用基于0的数组,而我的数组中只有9个元素,那么什么超出了范围?

这是我的电话:

let allvalues = employeefirstname & "," & employeelastname & "," & Convert.ToString(userID) & "," & JobSite & "," & abcd & "," & efg & "," & Convert.ToString(habib) & "," & ToString(alpha) & "," & departurereason & "," & ToString(departuredate)

我的电话:

" http://yourwebsiteinformationhere.aspx?passedin= ", allvalues

和我的代码后面:

try {
        string Info = Request["allvalues"];
        string[] spl = Info.Split(',');
        //Generate SMPT email here
        msg.Body = spl[1] + " " + spl[2] + " " + spl[3]+ " " + spl[4] + " " + spl[5]+ " " + spl[6]+ " " + spl[7]+ " " + spl[8]+ " " + spl[9]
//send mail here
}
catch (Exception exception)
{
   throw exception;
}

数组包含9个元素,但超出范围

c#中的数组是从零开始的,所以它们从0到Count-1(在你的例子中是8):

msg.Body = spl[0] + " " + spl[1] + " " + spl[2] + " " + spl[3]+ " " + spl[4] + " " + spl[5]+ " " + spl[6]+ " " + spl[7]+ " " + spl[8]+ " ";

但是一个不依赖于数组大小的更安全的方法是使用string.Join:

msg.Body = string.Join(" ", spl);

加上你正在拉错误的查询字符串@sstan注意到

应该是:

Request["passedin"];

:

Request["allvalues"];