title: How to initialize a List<T> to a given size (as opposed to capacity)?
body: <p>.NET offers a generic list container whose performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization. </p>
  
  <p>Arrays are very easy to initialize with a default value, and by definition they already have certain size:</p>
  
  <pre><code>string[] Ar = new string[10];
  </code></pre>
  
  <p>Which allows one to safely assign random items, say:</p>
  
  <pre><code>Ar[5]="hello";
  </code></pre>
  
  <p>with list things are more tricky. I can see two ways of doing the same initialization, neither of which is what you would call elegant:</p>
  
  <pre><code>List&lt;string&gt; L = new List&lt;string&gt;(10);
  for (int i=0;i&lt;10;i++) L.Add(null);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>string[] Ar = new string[10];
  List&lt;string&gt; L = new List&lt;string&gt;(Ar);
  </code></pre>
  
  <p>What would be a cleaner way?</p>
  
  <p>EDIT: The answers so far refer to capacity, which is something else than pre-populating a list. For example, on a list just created with a capacity of 10, one cannot do <code>L[2]="somevalue"</code></p>
  
  <p>EDIT 2: People wonder why I want to use lists this way, as it is not the way they are intended to be used. I can see two reasons:</p>
  
  <ol>
  <li><p>One could quite convincingly argue that lists are the "next generation" arrays, adding flexibility with almost no penalty. Therefore one should use them by default. I'm pointing out they might not be as easy to initialize.</p></li>
  <li><p>What I'm currently writing is a base class offering default functionality as part of a bigger framework. In the default functionality I offer, the size of the List is known in advanced and therefore I could have used an array. However, I want to offer any base class the chance to dynamically extend it and therefore I opt for a list.</p></li>
  </ol>