8

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 };
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
Ravi Gupta
  • 6,258
  • 17
  • 56
  • 79
  • What is wrong using a loop? – Soner Gönül Dec 28 '12 at 12:54
  • 2
    this is interesting, but can you also give some context on why you need that and how you plan to use it? Just that we can suggest you a good solution of your problem without being framed by your suggested approach :) – Davide Piras Dec 28 '12 at 12:55
  • doing something repitive without using a loop is like juggling without using your arms. Why can't you use a loop construct? – rene Dec 28 '12 at 12:56
  • You can not insert without loop you can insert in it other with InsertRange. – Reno Dec 28 '12 at 12:56
  • if you can create array, then you can create list of string from the array. http://stackoverflow.com/questions/157944/how-to-create-arraylist-arraylistt-from-array-t-in-java – darkapple Dec 28 '12 at 12:57
  • @rene why use a loop when Enumerrable.Repeat is created specifically for a situation like this? – Rune FS Dec 28 '12 at 13:00
  • @RuneFS Isn't that an highlevel contruct over a loop? In my reasoning I guess the OP want to rule out the msil containing a branch statement. But maybe that is just the purist in me... :) – rene Dec 28 '12 at 13:08
  • @rene there's definitively going to be a branch or two in there but he said "loop" and not "loops or conditions" if he worried about branches then if,?:,switch,goto,continue,break and possibly a fwe other should have been banned as well – Rune FS Dec 28 '12 at 13:27

2 Answers2

25

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.

igrimpe
  • 1,775
  • 11
  • 12
  • its already there in CLR. But less used method. short and sweet to repeat :) – Ravi Gadag Dec 28 '12 at 12:58
  • Repeat doesn't need to be implemented with a loop. It could be recursive, use the enumerator block rewrite (which is rewritten to a state machine), use labels and goto's etc – Rune FS Dec 28 '12 at 13:07
  • 1
    @RuneFS: Can you give me a valid definition of "loop"? If you work with goto/label you still have a "loop" I would say. I don't know if it's correct, but I would define a "loop" (in programming) as a pattern used to `repeat` (the execution of) a piece of code. And if you do something wrong with recursion, you usually get an endless "loop", don't you? – igrimpe Dec 28 '12 at 13:11
  • @igrimpe usually that's calle diteration and it's a endless recursion. To me if that's the definition of a loop the you could also say that when coding in C# you are actually coding in machine code because the end result is machine code. So asking how you can repeat something with out repeating (your definition of a loop) doesn't really make sense does it? In a given context I'd agree with your definition. Here however I believe `loop` is short hand of loop construct (`for`,`foreach`, `do`, `while`) because then there's still sense in the question – Rune FS Dec 28 '12 at 13:24
  • @RuneFS: We could discuss this endless (which SO dislikes), but you are right in one thing: The question (How can I loop without looping) doesn't make sense. I understand the question more as "how can I make it look NICE?". And LINQ (imho) looks nice ;) – igrimpe Dec 28 '12 at 14:12
0

Try doing this:

List<String> list = new List<String>();
for(int i=0; i<N; i++)
{
  list.add("str");
}
SalmaanKhan
  • 811
  • 7
  • 4