How to print the contents of an array to a label in c# [duplicate]

不羁岁月 提交于 2019-12-30 10:45:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!