C# 目标数组不够长。检查destinex和length以及数组';s下界
我有一个具有以下代码的类:C# 目标数组不够长。检查destinex和length以及数组';s下界,c#,arrays,exception,C#,Arrays,Exception,我有一个具有以下代码的类: Array tags; if (lines.Length > 0) { configText = lines[0]; tags = new Array[lines.Length]; lines.CopyTo(tags,1); } 这里我得到了以下错误: 目标数组不够长。检查destinex和长度以及 数组的下界 方法: private bool ReadPointListFile(string fi
Array tags;
if (lines.Length > 0)
{
configText = lines[0];
tags = new Array[lines.Length];
lines.CopyTo(tags,1);
}
这里我得到了以下错误:
目标数组不够长。检查destinex和长度以及
数组的下界
方法:
private bool ReadPointListFile(string fileName) {
// Read each line of the file into a string array. Each element
// of the array is one line of the file.
string[] lines = System.IO.File.ReadAllLines(fileName);
string configText = string.Empty;
if (lines.Length > 0)
{
configText = lines[0];
tags = new Array[lines.Length];
lines.CopyTo(tags,1);
}
else
lines.CopyTo(tags,0);
GetConfigurationInfo(lines[0], out this.sInterval, out this.dataAggregate);
return true;
}
它将从1个索引开始复制,而不是从零索引开始复制,这会造成问题。 试一试
标记是数组对象的数组,这可能不是您想要的。 如果要从字符串数组(行)复制,目标数组也应该是字符串数组。 所以,无论您在哪里声明标记,它都应该是
string[]标记代码>
在if块中,它应该是tags=newstring[lines.Length]代码>
这是关于类型和ArrayTypeMismatch异常的部分
现在,如果您打算复制除第一个元素以外的所有元素,则不能使用CopyTo(标记,1)
,因为1代表目标数组。它指示从何处开始写入值。这就是为什么你有例外。
相反,只需执行以下循环:
对于(int i=1;itags=newstring[lines.Length-1]代码>
如果您想跳过行和标记数组中的第一个索引,那么它就是tags=newstring[lines.Length]代码>和标记[i-1]=行[i]代码>对于上述代码:ArrayTypeMismatch异常发生行的类型是什么。。它是数组还是列表?向我们展示整个代码。例如,“行”的类型及其内容等
lines.CopyTo(tags,0);