Alex Rivera | Logout

C#: Build List of years as integers

Asked 2010-11-06T17:51:40.453
19

I'd like to create a C# List of integers, from say 1930 to 2010. Off the top of my head, the only way I can think of to do this is to use a for or while loop to loop through each integer between the numbers and add them to the List individually.

I know C# lists have a lot of interesting methods, especially when you're using Linq. Can anyone think of a more efficient way of doing this?

Edit
Report

1 Answer

10

Use Enumerable.Range()

var MyList = Enumerable.Range(1930, 2010-1930+1).ToList();
answered 2010-11-06T17:55:43.610

Your Answer