Declaring an array with incremental values ​​- is there a shortcut?

This question is probably pretty dumb, but I'm new to C # and I'm not sure if there are any shortcuts for this. I have a dynamic array for which the range will always be 1-n, with n being a variable. Do I need to declare an array and contain incremental values ​​without loops?

Think along the lines of my array holding values ​​of 1-50. I would like to declare the array as such (logically): double[] myArray = new double[] {1-50} or, more general for my purposes, double[] myArray = new double[] {1-n} . I do not know what made me think about it, I just thought that I would ask.

I am going to associate this array (or list) with a combo box in WPF. I think combo-box installation will also work if there is a shortcut for this.

Sorry for the dumb question. =)

+6
source share
3 answers
 int n = 50; var doubleArray = Enumerable.Range(1, n).Select(x => (double)x).ToArray(); 

This will create a sequence of integers from 1 to n (in this case 50), and then drop each of them to double and create an array of these results.

+10
source

You can use List<T> , which represents a dynamic array to which you could add elements.

+3
source

System.Linq.Enumerable.Range can generate an nn int enum. List the listing if you really want to double.

 System.Linq.Enumerable.Range(1,20).ToArray() 

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx

0
source

Source: https://habr.com/ru/post/896152/


All Articles