Skip to main content

How to Make a Sequence of Numbers with Enumerable.Range in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Enumerable.Range(1, 5) is the numbers 1, 2, 3, 4 and 5. It's the quick way to get a run of whole numbers for a loop, a test list or a set of labels.

Microsoft's reference: Generates a sequence of integral numbers within a specified range. The numbers go up by one, from the start, for as many numbers as you ask for.

Syntax​

Syntax
IEnumerable<int> <numbers> = Enumerable.Range(<start>, <count>);
PartWhat it is
<start>The first number
<count>How many numbers: not the last one
<numbers><start>, <start> + 1 and so on, <count> numbers in all

The Example​

A console app called NumberRange makes the numbers 1 to 5, three years from 2024, and three labels made from a range.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n NumberRange
cd NumberRange
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/NumberRange/NumberRange.csproj:
Determining projects to restore...
Restored /Users/you/source/NumberRange/NumberRange.csproj (in 50 ms).
Restore succeeded.

dotnet new console -n NumberRange creates a folder named NumberRange with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or NumberRange.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the three ranges:

Program.cs (the ranges)
IEnumerable<int> oneToFive = Enumerable.Range(1, 5);
IEnumerable<int> years = Enumerable.Range(2024, 3);
IEnumerable<string> labels = Enumerable.Range(1, 3).Select(number => $"Book {number}");
  • Enumerable.Range(1, 5) is five numbers from 1.
  • Enumerable.Range(2024, 3) is three numbers from 2024.
  • .Select(number => $"Book {number}") turns each number into a label.

Then print them:

Program.cs (the output)
Console.WriteLine(string.Join(", ", oneToFive));
Console.WriteLine(string.Join(", ", years));
Console.WriteLine(string.Join(", ", labels));

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
1, 2, 3, 4, 5
2024, 2025, 2026
Book 1, Book 2, Book 3

macOS-style terminal window titled Terminal — NumberRange showing dotnet run printing 1, 2, 3, 4, 5, then 2024, 2025, 2026, then Book 1, Book 2, Book 3

Each range starts at its first number and has exactly as many numbers as its count.

The Complete File​

macOS-style code window titled Program.cs — NumberRange showing Enumerable.Range from 1 for 5 numbers, from 2024 for 3 numbers, and from 1 for 3 numbers turned into Book labels with Select, and three Console.WriteLine calls

Program.cs
IEnumerable<int> oneToFive = Enumerable.Range(1, 5);
IEnumerable<int> years = Enumerable.Range(2024, 3);
IEnumerable<string> labels = Enumerable.Range(1, 3).Select(number => $"Book {number}");

Console.WriteLine(string.Join(", ", oneToFive));
Console.WriteLine(string.Join(", ", years));
Console.WriteLine(string.Join(", ", labels));

Common Mistakes​

MistakeWhat happensDo this instead
Reading the second number as the last oneEnumerable.Range(5, 10) is ten numbers from 5: 5 to 14Pass the count: for 5 to 10, Enumerable.Range(5, 6) returned 5, 6, 7, 8, 9, 10
A negative countArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'count')Use 0 or more: Enumerable.Range(1, 0) returned an empty sequence

FAQ​

How do I count in steps of 2?​

Multiply with Select: Enumerable.Range(0, 5).Select(number => number * 2) returned 0, 2, 4, 6, 8.

Conclusion​

Enumerable.Range(start, count) gives count whole numbers from start. Remember that the second number is a count, and use Select to turn the numbers into steps, labels or anything else.

Additional Resources​