Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

September 4, 2009

Enumerable.Range

Filed under: C#,Lambda Expressions,VB.NET @ 2:38 pm

The Enumerable class is new in .NET 3.5 and is part of the System.Linq namespace. It provides a set of static methods that allow you to query any object that implements IEnumerable, basically meaning any object that supports a for/each.

This post focuses on the Range method of the Enumerable class and some of the helpful things that this class can do for you.

NOTE: Be sure to set a reference to System.Core.

First, at the top of your code file, add this code.

In C#:

using System.Linq;

In VB:

Imports System.Linq

(Or for VB you can import a namespace using the project properties.)

You can then use the Enumerable.Range as shown in the following examples.

In C#:

// Initialize an array of numbers 0 – 9
int[] numbers = Enumerable.Range(0, 10).ToArray();

// Initialize an array with any arithmetic sequence
// This one does 10, 20, 30 … 100
int[] arithmetic = Enumerable.Range(0, 10).Select(
                    i => 10 + (10 * i)).ToArray();

// Initialize an array with any geometric sequence
// This one does 2, 6, 18, …  39366
int[] geometric = Enumerable.Range(0, 10).Select(
                    i => 2 * (int)Math.Pow(3,i)).ToArray();

// This one does 10, 5, 2.5, … 0.01953125
double[] geometric2 = Enumerable.Range(0, 10).Select(
                    i => 10 * Math.Pow(.5, i)).ToArray();

// Initialize an array with letters
// This one does A, B, … J
string[] letters = Enumerable.Range(0, 10).Select(
                    i => ((char)(‘A’ + i)).ToString()).ToArray();

// This one does z, y, … q
string[] letters2 = Enumerable.Range(0, 10).Select(
                    i => ((char)(‘z’ – i)).ToString()).ToArray();

In VB:

‘ Initialize an array of numbers 0 – 9
Dim numbers() As Integer = Enumerable.Range(0, 10).ToArray()

‘ Initialize an array with any arithmetic sequence
‘ This one does 10, 20, 30 … 100
Dim arithmetic() As Integer = Enumerable.Range(0, 10).Select( _
          Function(i) 10 + (10 * i)).ToArray()

‘ Initialize an array with any geometric sequence
‘ This one does 2, 6, 18, …  39366
Dim geometric() As Integer = Enumerable.Range(0, 10).Select( _
          Function(i) CType(2 * (3 ^ i), Integer)).ToArray()

‘ This one does 10, 5, 2.5, … 0.01953125
Dim geometric2() As Double = Enumerable.Range(0, 10).Select( _
          Function(i) 10 * (0.5 ^ i)).ToArray()

‘ Initialize an array with letters
‘ This one does A, B, … J
Dim letters() As String = Enumerable.Range(0, 10).Select( _
          Function(i) (Chr(Asc("A") + i)).ToString()).ToArray()

‘ This one does z, y, … q
Dim letters2() As String = Enumerable.Range(0, 10).Select( _
          Function(i) (Chr(Asc("z") – i)).ToString()).ToArray()

Note: Even though all of the above examples use ToArray to build an array, you could instead build lists using ToList.

The first example uses the basics of the Range to build an array of integers. You can use this syntax to build any range of integers.

The other examples combine the Range method with a lambda expression to build other types of numeric arrays.

The arithmetic sequence example demonstrates how to build an array where the difference between each term is a constant. In this example, the constant is 10.

The geometric sequence examples demonstrates how to build any array where each term after the first is generated by multiplying the previous one by a fixed non-zero number (common ratio). In the first example, the common ratio is 3. In the second example, the common ratio is 1/2.

The alphabetic sequence uses the fact that the Unicode characters for the letters in the alphabet are in numerical sequence. So adding 1 to ‘A’ gives you ‘B’ and so on.

Use the Range method any time you want to build a list or array of values in a logical sequence.

Enjoy!

3 Comments

  1.   Waleed El-Badry — September 4, 2009 @ 9:10 pm    Reply

    You still impress me with your magical way in your posts. However, I blame using lambda expression since it makes me lose focus on the feature you try to explain. I’ve noticed that you fell in love with the anonymous function as you used it intensively in your last posts.

    Thanks again and stay brilliant as you are 🙂

  2.   Joacim — September 6, 2009 @ 8:33 pm    Reply

    If you don’t like to use lambda expression you can just as well use a regular LINQ query and let the compiler create the lambda for you.

    ‘ Initialize an array with letters
    ‘ This one does A, B, … J
    Dim letters() As String = (From i In Enumerable.Range(0, 10) _
    Select (Chr(Asc(“A”) + i).ToString())).ToArray()

  3.   Wes McClure — September 6, 2009 @ 11:37 pm    Reply

    A spin off of the python range method?

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2022 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs