问题
I want to display the contents of the Array from a label with a comma between each number. num1 - num6 are integer variables converted from textboxes. So Far I have done this.
int[] number = new int [6] {num1, num2, num3, num4, num5, num6};
Array.Sort(number);
lblAnswer3.Text = number.ToString();
The output of this code is: System.Int32[]
I would like the output to be: num1, num2, num3, num4, num5, num6 in ascending order.
回答1:
You can easily concat IEnumerables and arrays with string.Join:
lblAnswer3.Text = string.Join(", ", number);
回答2:
You can do it using Linq:
lblAnswer3.Text = number.OrderBy(x => x).Select(x => x.ToString()).Aggregate((a, b) => a + ", " + b);
来源:https://stackoverflow.com/questions/26434458/how-to-print-the-contents-of-an-array-to-a-label-in-c-sharp