Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to initialize an Array of N numbers following a simple pattern?

Tags:

c#

Let's say the first N integers divisible by 3 starting with 9.

I'm sure there is some one line solution using lambdas, I just don't know it that area of the language well enough yet.

like image 378
George Mauer Avatar asked Dec 14 '22 06:12

George Mauer


1 Answers

Just to be different (and to avoid using a where statement) you could also do:

var numbers = Enumerable.Range(0, n).Select(i => i * 3 + 9);

Update This also has the benefit of not running out of numbers.

like image 64
Cameron MacFarland Avatar answered Dec 21 '22 22:12

Cameron MacFarland