Creating an array of sequential numbers

suggest change

LINQ provides a method that makes it easy to create a collection filled with sequential numbers. For example, you can declare an array which contains the integers between 1 and 100.

The Enumerable.Range method allows us to create sequence of integer numbers from a specified start position and a number of elements.

The method takes two arguments: the starting value and the number of elements to generate.

Enumerable.Range(int start,int count)

Note that count cannot be negative.

Usage:

int[] sequence = Enumerable.Range(1, 100).ToArray();

This will generate an array containing the numbers 1 through 100 ([1, 2, 3, ..., 98, 99, 100]).

Because the Range method returns an IEnumerable<int>, we can use other LINQ methods on it:

int[] squares = Enumerable.Range(2, 10).Select(x => x * x).ToArray();

This will generate an array that contains 10 integer squares starting at 4: [4, 9, 16, ..., 100, 121].

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Arrays:
* Arrays
* Creating an array of sequential numbers

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script