Posted by: Cirilo Meggiolaro | 01/12/2009

Tip of the day #90 – Generating numeric sequential lists

Today’s tip is quick and straightforward. How to generate a sequential list or array of integers based on a numeric range.

The Enumerable class has a method called Range that retrieves an IEnumerable<int> based on a numeric range. Once you have the IEnumerable object you can convert it to a list of integers or an array of integers.

Range Method

The first parameter is the first number of the range. The second number is the length of this sequence.

public static IEnumerable<int> Range(int start, int count);

How to…

The following code snippet creates a list of integers from 0 to 29. The solution is pretty straightforward and self-explanatory:

List<int> list = Enumerable.Range(0, 30).ToList();

int[] list = Enumerable.Range(0, 30).ToArray();

Advertisements

Responses

  1. Simple and useful!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: