This post will discuss how to initialize an Int array with a range of numbers in C#.

1. Using Enumerable.Range Method

The standard solution to generate a sequence of numbers within a specified range is using the Enumerable.Range method from System.Linq namespace. It takes the starting value of the sequence and the number of sequential integers to generate. The following example demonstrates its usage.

Download  Run Code

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

2. Using For Loop

A faster solution is to create a new array of the required size and use a traditional for-loop to fill up the array, as shown below:

Download  Run Code

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

That’s all about initializing an Int array with a range of numbers in C#.