Search code examples
c#.net-3.0

How to generate a list of number in C#


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


Solution

  • 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());