If there's already a way to get a List<int> of consecutive integers without a loop in C#, I don't know what it is, so I created a method for it.
public static List<int> GetIntegerListFromRangeUsingLoop(int start, int end) {
if (end < start) {
throw new ArgumentException("Faulty parameter(s) passed: lower bound cannot be less than upper bound.");
}
List<int> returnList = new List<int>(end - start + 1);
for(int i = start; i <= end; i++) {
returnList.Add(i);
}
return returnList;
}
UPDATE:
I was pointed to Enumerable.Range(), so I updated my code.
public static List<int> GetIntegerListFromRangeUsingEnumerableRange(int start, int end) {
IEnumerable<int> list = Enumerable.Range(start, end - start + 1);
return list.ToList();
}
Then I used the StopWatch class in my unit tests to compare the two methods.
Results:
GetIntegerListFromRangeUsingEnumerableRange averaged 6.5 milliseconds.
GetIntegerListFromRangeUsingLoop averaged 0.43 milliseconds.
Wow. Apparently Enumerable.Range() is a lot slower than using a loop.