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.
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);
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();