Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 目标数组不够长。检查destinex和length以及数组';s下界_C#_Arrays_Exception - Fatal编程技术网

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);