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

时间:2015-09-24 13:08:59

标签: c# asp.net

我收到此错误,即使我的代码设置为处理适当数量的参数。 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;
}

2 个答案:

答案 0 :(得分:4)

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所说,你正在拉错了查询字符串。

答案 1 :(得分:3)

应该是:

Request["passedin"];

Request["allvalues"];