Easiest way to create a list of <int> ordered numbers in C #?

This does not seem to be a problem:

List<int> steporders = new List<int>();
for (int i = 1; i <= 10; i++)
{
 steporders.Add(i);
}

But I see there a simpler, better or smarter way that I did not think about. There is?

+3
source share
2 answers

Yes, you can do it easily:

List<int> steporders = Enumerable.Range(1, 10).ToList();
+19
source

My friend always thought I should not answer questions before I find out what this is about: you have one of them :)

Why do you need a list with numbers from 1 to 10? This seems completely useless (at least without any context).

-1
source

Source: https://habr.com/ru/post/1726088/


All Articles