Back to
Standard Query OperatorsEditIntroduction
The Range operator generates a sequence of integral numbers.
EditMethod Signatures
public static IEnumerable<int> Range(int start, int count)
EditExceptions
Throws an ArgumentOutOfRange exception if
count < 0, or
start +
count > int.MaxValue.
EditPseudo-code
If
count < 0 throw an ArgumentOutOfRange exception.
If
start +
count > int.MaxValue, throw an ArgumentOutOfRangeException.
Begin loop starting at 0.
Return an integer value equal to (start + loop counter). Resume execution from here when the next element is requested.
EditLoop count
0. This is a generation operator.This operator implements the standard
deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.
EditCode Samples
// Example showing how to generate a range starting at the value 10, for 20 elements. Added by Rob Philpott
using System;
using System.Linq;
namespace HookedOnLinq
{
public class RangeExample
{
public static void Main()
{
var numbers = Enumerable.Range(10, 20);
foreach (int x in numbers)
{
Console.WriteLine(x);
}
Console.ReadLine();
}
}
}
returns:
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29EditPractical Example With Windows Forms
This example will fill a range of numbers in a ComboBox. You can use this for displaying years, day numbers or coordinates.
xComboBox.DataSource = Enumerable.Range(0, 15).ToArray();
yComboBox.DataSource = Enumerable.Range(0, 15).ToArray();
There is a screenshot at the article:
LINQ Enumerable.Range Practical Example (by Sam Allen)