-1

How to create a list of num from 1 to 10 Example:

int[] values = Enumerable.Range(1,max).ToArray();
MessageBox.Show(values+",");

The output should be: 1,2,3,4,5,6,7,8,9,10 Please help

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
ZeroX
  • 49
  • 1
  • 2
  • 8

3 Answers3

6

your code is generating array of integers from 1 to 10

int[] values = Enumerable.Range(1,10).ToArray();

but you're displaying them in wrong way (you're trying to cast int array to string), change it to

MessageBox.Show(string.Join(",", values);

string.Join will join your values separating them with ,

In .Net <4.0 you should use (and I believe OP is using one)

MessageBox.Show(string.Join(",", values.Select(x=>x.ToString()).ToArray());
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
0
List<int> values = Enumerable.Range(1, 10).ToList();
MessageBox.Show(string.Join(",", values.Select(x => x.ToString())));
Sender
  • 6,660
  • 12
  • 47
  • 66
Arash
  • 889
  • 7
  • 18
0

Try like below using the generic version of Join<T>() method.

        int[] arr = Enumerable.Range(1, 10).ToArray();
        MessageBox.Show(string.Join<int>(",", arr));

Generate 1,2,3,4,5,6,7,8,9,10

(OR) using good old foreach loop

        string str = string.Empty;
        foreach (int i in arr)
        {
            str += i.ToString() + ",";
        }
        MessageBox.Show(str.TrimEnd(','));
Rahul
  • 76,197
  • 13
  • 71
  • 125