Range
System.Linq.EnumerableMethod
public static
IEnumerable<int>
Range
(int
start
, int
count
)
Description
Generates a sequence of integral numbers within a specified range.
Remarks
The Range
method returns an IEnumerable<int>
that contains a sequence of integral numbers starting from the specified start
value and continuing for the specified count
number of values. The sequence will be start
, start + 1
, start + 2
, and so on, up to start + count - 1
.
If count
is zero or negative, an empty sequence is returned.
Parameters
-
start
The value of the first integer in the sequence.
-
count
The number of integral numbers to generate.
Returns
An IEnumerable<int>
that contains integers in the specified range.
Exceptions
-
ArgumentOutOfRangeException
count
is less than zero. -
ArgumentOutOfRangeException
start
andcount
result in an overflow.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main(string[] args)
{
// Generate a sequence of 10 integers starting from 5
IEnumerable<int> numbers = Enumerable.Range(5, 10);
// Output the numbers
foreach (int number in numbers)
{
Console.Write(number + " ");
}
// Output: 5 6 7 8 9 10 11 12 13 14
Console.WriteLine();
// Generate an empty sequence
IEnumerable<int> emptySequence = Enumerable.Range(1, 0);
Console.WriteLine($"Empty sequence count: {emptySequence.Count()}");
// Output: Empty sequence count: 0
}
}