我发现了一段代码应该会产生错误,因为数组超出了界限,但它的行为却很奇怪。当我踏入它时,它会在 4 个循环后停止并且不添加控件。这怎么可能,没有错误或编译器警告或任何东西?
Panel[] panel = new Panel[4];
Label[] label = new Label[4];
private void Form1_Load(object sender, EventArgs e)
{
for (int x = 0; x < 20; x++)
{
label[x] = new Label { Name = x.ToString("00"), BackColor = Color.Blue, Text = "Test" };
panel[x] = new Panel { Name = x.ToString("00"), BackColor = Color.Blue };
}
tableLayoutPanel1.Controls.AddRange(label);
tableLayoutPanel2.Controls.AddRange(panel);
}