3

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
  • 1
    use Join method of array. Commented Oct 17, 2014 at 22:53

2 Answers 2

6

You can easily concat IEnumerables and arrays with string.Join:

lblAnswer3.Text = string.Join(", ", number);
Sign up to request clarification or add additional context in comments.

3 Comments

+1 I didn't realize String.Join could work on IEnumerable<T>
@BradleyDotNET that overload was introduced in .NET 4
In .NET2 you need something like string.Join(","; Array.ConvertAll(number, delegate(int i) { return i.ToString(); }));
0

You can do it using Linq:

lblAnswer3.Text = number.OrderBy(x => x).Select(x => x.ToString()).Aggregate((a, b) => a + ", " + b);

8 Comments

-1. All of that is unnecessary.
@JohnSaunders Kind of harsh to down-vote a solution that works, isn't it? Shouldn't we just upvote the solutions that we like the best and down vote ones that either don't work or don't answer the question?
@RufusL I'm not sure I would downvote it, but this solution is over-complicated enough that I could understand it being regarded as "not useful". The OrderBy might be required for ascending order though (if they aren't already ordered).
@RufusL: the purpose of scoring answers is so that future readers are more likely to choose answers with higher scores. I want to make sure future readers are less likely to choose this one. If the OP is concerned about rep points, then the OP can delete the answer and get the two points back.
"the purpose of scoring answers is so that future readers are more likely to choose answers with higher scores" stating the obvious in a condescending tone only makes you look like a jerk. the answer is not wrong. you can argue it's not as efficient but it's an alternative
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.