HomeSearch

C# Enumerable.Range (Get Range of Numbers)

This C# example program uses the Range method on the Enumerable type. It requires System.Linq.

Enumerable.Range.

A sequence of numbers progresses in order: 10, 11, 12, 13. With Enumerable.Range, each number is one greater than the previous.

Two arguments.

In some languages (like Python) we specify indexes: a first and last index. But with Enumerable.Range we specify a start index and a count of elements for the final sequence.

First example.

Let us begin. We invoke Enumerable.Range (part of the System.Linq namespace). We use foreach to loop over all the numbers in the resulting sequence.Foreach

First argument: This is the start index. It can be negative or positive. Here we start at the int 5.

Second argument: This is the count of elements to appear in the final sequence (not a last index). We specify 3 to have 3 elements.

C# program that uses Enumerable.Range using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { // Generate a range of number starting at 5. // ... First parameter is the start number. // Second parameter is the count of numbers (not the last index). IEnumerable<int> numbers = Enumerable.Range(5, 3); foreach (int n in numbers) { Console.WriteLine(n); } } } Output 5 6 7

Count error.

The second argument to Enumerable.Range must be 0 or greater. Here we get an error when we try to create a sequence of negative 1 length.
C# program that causes exception using System.Linq; class Program { static void Main() { // This will cause an exception. var numbersNegative = Enumerable.Range(10, -1); } } Output Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: count at System.Linq.Enumerable.Range(Int32 start, Int32 count) at Program.Main()...

Windows Forms example.

This code is a little different. We use a call to Enumerable.Range to create a control's contents in Windows Forms.

Tip: With Enumerable.Range, we can simplify numeric lists and controls in Windows Forms programs.

ToArray: We generate new int arrays with ToArray. The example fills 2 Windows Forms menus with the numbers between 0 and 14 inclusive.

Int ArrayToArray

Note: The programming term for notation that expresses an entire list at once is list comprehension.

C# program that uses Enumerable.Range public partial class ReporterWindow : Form { public ReporterWindow() { // // Autogenerated code. // InitializeComponent(); // // Add "using System.Linq;" at the top of your source code. // ... Use an array from Enumerable.Range. // ... Set it to the data source of the DropDown boxes. // xComboBox.DataSource = Enumerable.Range(0, 15).ToArray(); yComboBox.DataSource = Enumerable.Range(0, 15).ToArray(); } }

Some notes.

The Range method, with ToArray, returns the entire array needed. This may be easier to read. The above example creates the arrays in one line of code each.

Further: With expression-based programming, we reduce source code size. Expressions encapsulate logic into a single statement.

LINQ

Some thoughts.

Imagine your project needed a range of numbers in thousands of places. With Enumerable.Range, it could be much smaller. And a range can be used with other LINQ methods.

Note: Thanks to Nathan Brink for some tips on improving this article. A description of arguments was added.

A warning.

Using the Enumerable.Range method will have performance negatives over iterative approaches due to the overhead of LINQ. These negatives must be considered.Initialize Array
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.