Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick way to initialize list of numbered strings?

How can I quickly create a string list with numbered strings?

Right now I'm 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's a quicker way to initialize such a string list, maybe in one or two lines?

like image 488
Martin Hansen Avatar asked Feb 14 '13 16:02

Martin Hansen


2 Answers

You could use LINQ:

Enumerable.Range(1, 10).Select(i => "This is string number " + i).ToList();
like image 94
Joey Avatar answered Sep 28 '22 04:09

Joey


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

like image 39
Parimal Raj Avatar answered Sep 28 '22 03:09

Parimal Raj