Building an integer sequence using the Range operator in LINQ C#

Say you need a list of integers from 1 to 10. The traditional way is the following:

List<int> traditional = new List<int>();
for (int i = 1; i <= 10; i++)

There’s a more concise way to achieve this with the static Range method which accepts two parameters. The first integer in the sequence and a count variable which defines the number of elements in the list. The step parameter is 1 by default and cannot be overridden:

IEnumerable<int> intRange = Enumerable.Range(1, 10);
foreach (int i in intRange)

This will print the integers from 1 to 10.

Say you need an integer list from 100 to 110 inclusive:

IEnumerable<int> intRange = Enumerable.Range(100, 11);

View the list of posts on LINQ here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: