title: Why assigning time in ArrayList and List is the same when adding 10000000 integers?
body: <p>I have the following code in which I compare the performance of the ArrayList and List through adding 10000000 integer numbers. In ArrayList I have boxing and unboxing, so in the first loop the time of adding integer numbers is much greater than in List but the time of assignig later the integer number to elements form list and arrayList is similar even though in arrayLIst I have to do unboxing. Why? Is it because boxing and unboxing of integers in ArrayList occurs before second loop? </p>
  
  <pre><code>int numbers = 10000000;
  ArrayList integerArrayList = new ArrayList();
  Stopwatch timer = new Stopwatch();
  timer.Start();
  
  for (int i = 0; i &lt; numbers; i++)
  {
      integerArrayList.Add(i); //niejawna konwersja na object
  }
  timer.Stop();
  var timeTestingIntegerArrayList = timer.ElapsedMilliseconds;
  Console.WriteLine($"The time for adding {numbers} numbers to Arraylist is: {timeTestingIntegerArrayList} ms");
  timer.Reset();
  
  timer.Start();
  
  for (int i = 0; i &lt; numbers; i++)
  {
  
      int number = (int)integerArrayList[i]; //unboxing
  }
  timer.Stop();
  var arrayListWriteTime = timer.ElapsedMilliseconds;
  Console.WriteLine($"The time for writting {numbers} numbers from Arraylist is: {arrayListWriteTime} ms");
  timer.Reset();
  
  Console.WriteLine($"The whole time for adding and writting {numbers} numbers from Arraylist is: {arrayListWriteTime + timeTestingIntegerArrayList} ms");` 
  
  int numbers = 10000000;
  
  List&lt;int&gt; integerList = new List&lt;int&gt;();
  Stopwatch timer = new Stopwatch();
  
  timer.Start();
  for (int i = 0; i &lt; numbers; i++)
  {
      integerList.Add(i);
  }
  timer.Stop();   
  var timeTestingIntegerList = timer.ElapsedMilliseconds;
  Console.WriteLine($"The time for adding {numbers} numbers to list is: {timeTestingIntegerList} ms");
  timer.Reset();
  
  timer.Start();
  for (var i = 0; i &lt; numbers; i++)
  {
  
      int number = integerList[i];
  
  }
  timer.Stop();
  var listWriteTime = timer.ElapsedMilliseconds;
  Console.WriteLine($"The time for assigning {numbers} numbers from list is: {listWriteTime} ms");
  timer.Reset();
  
  
  Console.WriteLine($"The whole time for adding and writting the integer numbers is: {timeTestingIntegerList + listWriteTime}");
  </code></pre>