title: Set/extend List<T> length in c#
body: <p>Given a <code>List&lt;T&gt;</code> in c# is there a way to extend it (within its capacity) and set the new elements to <code>null</code>? I'd like something that works like a <code>memset</code>. I'm not looking for sugar here, I want fast code. I known that in C the operation could be done in something like 1-3 asm ops per entry.</p>
  
  <p>The best solution I've found is <a href="https://stackoverflow.com/questions/481334/best-c-data-structure-for-random-order-population#481350">this</a>:</p>
  
  <pre><code>list.AddRange(Enumerable.Repeat(null, count-list.Count));
  </code></pre>
  
  <p>however that is c# 3.0 (&lt;3.0 is preferred) and might be generating and evaluating an enumerator.</p>
  
  <p>My current code uses:</p>
  
  <pre><code>while(list.Count &lt; lim) list.Add(null);
  </code></pre>
  
  <p>so that's the starting point for time cost.</p>
  
  <p>The <a href="https://stackoverflow.com/questions/481334/best-c-data-structure-for-random-order-population">motivation for this</a> is that I need to set the n'th element even if it is after the old Count. </p>