Quick way to initialize a list of numbered lines?

How to quickly create a list of lines with numbered lines?

Now I am using:

var str = new List<string>();

for (int i = 1; i <= 10; i++)
{
    str.Add("This is string number " + i);
}

This works, however, I wonder if there is a faster way to initialize such a list of strings, possibly in one or two lines?

+5
source share
2 answers

You can use LINQ:

Enumerable.Range(1, 10).Select(i => "This is string number " + i).ToList();
+5
source
var str = Enumerable.Range(1, 10).Select(i => "This is string number " + i).ToList();

+3
source

All Articles