Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a List<string> containing a single string N times

Tags:

c#

.net

Is there any way to construct a list of type List<string> which contains a single string N times without using a loop? Something similar to String(char c, int count) but instead for List of strings.

List<string> list = new List<string>() { "str", "str", "str", ..... N times };
like image 669
Ravi Gupta Avatar asked Dec 28 '12 12:12

Ravi Gupta


1 Answers

You can use Repeat():

List<String> l = Enumerable.Repeat<String>("foo", 100).ToList<String>();

It will still use a loop of course, but now you don't "see" it.

like image 105
igrimpe Avatar answered Sep 21 '22 07:09

igrimpe