将未知大小的数组复制到另一个数组

时间:2011-09-15 22:06:04

标签: c# arrays

我有一组数字1 - 1000000(一百万+没有特定的顺序) 我想将数组复制到另一个数组

 string[] lines = System.IO.File.ReadAllLines("DeadPeopleTZ.rpt");
 textBox1.AppendText(myInt.ToString() + "\n");
             string[] target;
             Array.Copy(lines, target);

2 个答案:

答案 0 :(得分:0)

可以创建适当大小的数组,然后使用Array.Copy

string[] target = new string[lines.Length];
Array.Copy(lines, target, lines.Length);

但克隆它会更容易:

string[] target = (string[]) lines.Clone();

答案 1 :(得分:0)

target数组所需的大小与lines的大小相同 - 因此运行时的大小未知:

string[] lines = System.IO.File.ReadAllLines("DeadPeopleTZ.rpt");
string[] target = new string[lines.Length];
Array.Copy(lines, target, lines.Length);