Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize an array of int with a range of numbers [duplicate]

Tags:

arrays

c#

I want to initialize an array of int and populate it with a range of numbers:

return new int[].Populate(30,50);

So then I would have an array with 30, 31, 32, 33... - 50

like image 395
Sam Avatar asked Mar 27 '12 23:03

Sam


2 Answers

You can use Enumerable.Range() to e.g. return an array of 21 integers starting at 30:

return Enumerable.Range(30, 21).ToArray();
like image 171
BrokenGlass Avatar answered Nov 13 '22 16:11

BrokenGlass


Use Enumerable.Range

var array = Enumerable.Range(30, 21).ToArray();

This will result in a sequence of 21 values starting at 30. Simply put, you get your { 30 ... 50 } array.

like image 26
Anthony Pegram Avatar answered Nov 13 '22 16:11

Anthony Pegram