When creating test data or generating options for dropdown lists (such as years, months, or days), you often need a collection of consecutive numbers. While it is possible to add numbers to a list using a for loop, using the Enumerable.Range method included in C# LINQ allows for much more concise and declarative code.
In this article, we will explain the correct usage and precautions of the Range method, using the generation of a “list of years” for a calendar feature or dropdown as an example.
Overview of the Enumerable.Range Method
Enumerable.Range is a static method that generates a sequence of integers (IEnumerable<int>) starting from a specified number for a specified count.
Syntax:
Enumerable.Range(int start, int count)
Important Note: The second argument is “the number of elements to generate”, not the “ending value.” Be careful not to confuse this.
Practical Code Example: Generating a Recent Year List
The following code is an example of generating a list of years for 10 years starting from a specified year. This scenario is common when creating data sources for “Year” dropdowns in web forms.
using System;
using System.Collections.Generic;
using System.Linq;
namespace CalendarUtilities
{
class Program
{
static void Main()
{
// Scenario:
// We want to create "Year" options for a user registration form.
// We need a list for the next 10 years starting from 2020 (2020-2029).
int startYear = 2020;
int count = 10;
// 1. Generate sequential numbers using Enumerable.Range
// Generates 'count' (10) numbers starting from 'startYear' (2020).
// Result: 2020, 2021, 2022, ..., 2029
IEnumerable<int> yearList = Enumerable.Range(startYear, count);
// Output results
Console.WriteLine($"--- Year List ({count} years from {startYear}) ---");
Console.WriteLine(string.Join(", ", yearList));
// 2. (Advanced) Combine with Select to format strings
// If you want to convert to a string list like "2020 Year" instead of raw numbers
var formattedYears = Enumerable.Range(startYear, count)
.Select(y => $"{y} Year");
Console.WriteLine("\n--- Formatted List ---");
Console.WriteLine(string.Join(" | ", formattedYears));
}
}
}
Execution Result
--- Year List (10 years from 2020) ---
2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029
--- Formatted List ---
2020 Year | 2021 Year | 2022 Year | 2023 Year | 2024 Year | 2025 Year | 2026 Year | 2027 Year | 2028 Year | 2029 Year
Technical Points and Applications
1. Argument Range Limits
The arguments for Enumerable.Range are of type int. If the generated numbers exceed int.MaxValue (for example, if start + count - 1 overflows), an ArgumentOutOfRangeException will occur at runtime. If you need to handle very large numbers, you need to implement your own generator using the long type.
2. Using it to Specify Iteration Count
Range can be used not only for generating number lists but also when you simply want to “repeat a process a specific number of times.” It is useful as a starting point for foreach loops or LINQ queries.
// Example of repeating 5 times to create objects
var newItems = Enumerable.Range(1, 5).Select(i => new Item { Id = i, Name = $"Item-{i}" });
3. Difference from the Repeat Method
There is a similar method called Enumerable.Repeat.
- Range: Generates sequential numbers that change, like
1, 2, 3... - Repeat: Repeats the same value a specified number of times, like
1, 1, 1...
Choose Repeat if you want to create an array filled with an initial value, and choose Range if you need indexes or sequential numbers.
Summary
By using the Enumerable.Range method, you can write number sequence generation in just one line. As long as you understand that the second argument is the “count” and not the “end value,” this is a basic technique applicable in many situations, such as pagination, calendars, and test data generation.
