数组元素可能没有设置
本文关键字:设置 数组元素 | 更新日期: 2023-09-27 17:52:48
在下面的方法中,我将字符串smdrext设置为tmp[3]。然而,tmp[3]有时似乎是空的,因为我得到"索引超出了数组的边界"。在我设置它之前,我可以更改它是否真的存在,以确保程序不会因此而再次停止吗?
public void WriteToCSV(string line, string path)
{
string[] tmp = line.Split(',');
string smdrext = tmp[3];
if (ext.Contains(Convert.ToString(smdrext)))
{
File.AppendAllText(path, line + "'n");
}
}
请尝试使用下面的代码片段。
public void WriteToCSV(string line, string path)
{
if (!string.IsNullOrEmpty(line))
{
string[] tmp = line.Split(',');
if (tmp.Length > 3)
{
string smdrext = tmp[3];
if (ext.Contains(Convert.ToString(smdrext)))
{
File.AppendAllText(path, line + "'n");
}
}
}
}