C# 打印标签中的数组元素
每次我点击按钮,结果都是“5”。我想遍历一个数组并显示C# 打印标签中的数组元素,c#,arrays,.net,C#,Arrays,.net,每次我点击按钮,结果都是“5”。我想遍历一个数组并显示resultLabel中的所有元素 protected void okButton_Click(object sender, EventArgs e) { int[] myArray = new int[5] { 1, 2, 3, 4, 5 }; for (int i = 0; i < myArray.Length; i++) { resultLabel.Text = String.Join("
resultLabel
中的所有元素
protected void okButton_Click(object sender, EventArgs e)
{
int[] myArray = new int[5] { 1, 2, 3, 4, 5 };
for (int i = 0; i < myArray.Length; i++)
{
resultLabel.Text = String.Join(" ", myArray[i].ToString());
}
}
protectedvoidok按钮\u单击(对象发送方,事件参数e)
{
int[]myArray=newint[5]{1,2,3,4,5};
for(int i=0;i
您不需要循环,只要循环就足够了,它将连接集合的成员:
您不需要循环,只要循环就足够了,它将连接集合的成员:
循环在列表的每个元素上迭代,但在每次迭代中,您形成的字符串仅包含该元素 建议您需要的是:
resultLabel.Text += " " + myArray[i].ToString();
循环在列表的每个元素上迭代,但在每次迭代中,您形成的字符串仅包含该元素 建议您需要的是:
resultLabel.Text += " " + myArray[i].ToString();
如果只想在标签中显示数组的内容,为什么需要迭代?像这样的东西应该有用
protected void okButton_Click(object sender, EventArgs e)
{
int[] myArray = new int[5] { 1, 2, 3, 4, 5 };
resultLabel.Text = String.Join(" ",myArray);
}
如果只想在标签中显示数组的内容,为什么需要迭代?像这样的东西应该有用
protected void okButton_Click(object sender, EventArgs e)
{
int[] myArray = new int[5] { 1, 2, 3, 4, 5 };
resultLabel.Text = String.Join(" ",myArray);
}
string.Join
是执行此操作的最佳选项。或者,另一个选项是聚合
resultLabel.Text = myArray.Aggregate("", (a, b) => a + " " + b).Trim();
string.Join
是执行此操作的最佳选项。或者,另一个选项是聚合
resultLabel.Text = myArray.Aggregate("", (a, b) => a + " " + b).Trim();