text: What.
  Well…no. Not really. MSDN is not exactly telling the whole truth here.
  So, a List<T> is a List of elements of type T that you can treat as an Array. Simple – we’re done.
  Hold your horses there! Are you sure?
  Microsoft released the Reference Sources for .NET and they can make interesting reading. One in particular:[^]
  That’s>
  Hang on a moment…
  private T[] _items;
  Does that mean what I think it does?;
  }
  Yes. A List<T> is an Array that you can treat as a List, not the other way round.
  That's a big difference: it implies some operations do not work as the class name might imply:
  And that's important, because it defines what the various operations you can do on a List<T> can cost in terms of time.
  The first thing to note is that as an array, a List<T> is very efficient to access individual elements via the index.
  i = myList[1234567];
  is a trivial operation: get the start of the array in memory from the myList reference, multiply the index value by the number of bytes in the type T, add it to the start and fetch the element value. In performance terms, it's (almost) as fast as a "straight" array - not quite, as there is some additional checking code added which makes it slightly slower.
  But the other operations which make a List<T> more useful than an Array all come with a cost - and that can be quite a large cost.
  When you us the Add method to add an item to a List<T>, it checks to see if there is room in the existing array, and if not it:
  Think about what this means, just in terms of memory allocation and usage:
  If you want to add 2 million elements to a List:
  So to get sufficient space to hold your data, .NET will do 20 separate memory allocations (using both the small and large object heaps), and allocate and copy 2,097,148 elements that you will never use.
  OK, that’s not a lot of memory these days – if the elements are reference types it’s 8 bytes per element for a 64bit system - but that means nearly twice the memory you need will be allocated unnecessarily, and the copy operations will not be trivial in time terms. It may even kick in the Garbage Collector a couple of times if your element count / size combination gets big enough. begining of a List<T> you potentially will copy the entire array twice in effect: once to allocate enough space, and then again to make room at the front.
  alt="Image 2" data-src="/KB/dotnet/870013/artInsMap-r-700.jpg" class="lazyload" style="cursor: pointer; border: 0; width: 700px; height: auto" onclick="imageCleanup.showFullImage('/KB/dotnet/870013/artInsMap.jpg')" data-sizes="auto" data->
  Deleting items
  Delete is better than Insert, becasue it doesn't need the check-the-size-and-copy operations - but it still requires the processor to move every single element of the array with an index greater than or equal to the value of the delete point index one place to the left.
  width="447px" alt="Image 3" data-src="/KB/dotnet/870013/artDelMap.jpg" class="lazyload" data-sizes="auto" data->
  Moving and copying memory is slow: there are hardware instructions in most processors to speed the operation up (mostly by removing the setup costs at the start of each instruction) but it still requires the processor to fetch the data from memory (or it's cache(s)) and write the value to a new location (which will normally be write-through on the cache rather than "held" internally).
  If you are copying or moving a lot of information, it is quite possible that you will exceed the processor caches, and that means that the data caches will end up containing nothing except your data! Bulk data copies are slow, and should be avoided at all costs - they can slow your whole system!
  For example, you might be trying to add elements of an existing array in reverse order, so instead of using Add, you use Insert with a zero index:
  int elements = 50000;
  int[] data = Enumerable.Range(1, elements).ToArray();
  long addSum = 0;
  long insertSum = 0;
  for (int i = 0; i < 20; i++)
  {
  Stopwatch s1 = new Stopwatch();
  s1.Start();
  List<int> lAdd = new List<int>();
  for (int index = 0; index < elements; index++)
  {
  lAdd.Add(data[index]);
  }
  s1.Stop();
  Stopwatch s2 = new Stopwatch();
  s2.Start();
  List<int> lInsert = new List<int>();
  for (int index = 0; index < elements; index++)
  {
  lInsert.Insert(0,data[index]);
  }
  s2.Stop();
  addSum += s1.ElapsedMilliseconds;
  insertSum += s2.ElapsedMilliseconds;
  Console.WriteLine("{0}:{1}", s1.ElapsedMilliseconds, s2.ElapsedMilliseconds);
  }
  Console.WriteLine("-----");
  Console.WriteLine("Add: Total = {0}\n Average = {1}", addSum, addSum / 20);
  Console.WriteLine("Insert: Total = {0}\n Average = {1}", insertSum, insertSum / 20);
  And the results we get:
  1:694
  0:632
  0:649
  0:636
  0:635
  0:632
  0:633
  0:623
  0:627
  0:635
  0:631
  0:632
  0:708
  0:654
  0:642
  0:629
  0:639
  0:636
  0:625
  0:629
  -----
  Add: Total = 1
  Average = 0
  Insert: Total = 12821
  Average = 641
  Speak for themselves!
  Over 600 times slower to do inserts than to add items. And bear in mind, these are only 50,000 integers we are moving here! (I initially tried with 500,000 to get a "reasonable" figure for the Add operations - but the Inserts crashed my debugger :omg: ).
  2019-03-18 Typos.
  2015-01-27 Original Version.
  2014-03-05 V1.0
  The first version got some strange comments from a very small number of readers: 13 out of 12,009 views were negative - and it seemed that having explanations of what an Array and a List were at the top meant that some people just didn't read beyond that, and immediately voted this a one. That's not a problem in itself - everyone is allowed an opinion - but it implies that they didn't get to the "meat" of the article and missed why the use of an Array for something called a List can cause problems if you don't know what is going on behind the scenes.
  So, the initial intro was ripped out, and replaced with a more detailed explanation of what actually happens when you use a List<T> - and particularly when you misuse a List<T>
  This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
  Array
  List<T>
  if ( ( Index < 0 ) || ( Index >= this.neighbor.Count ) )
  {
  throw ( new System.ArgumentOutOfRangeException ( "Index" , Index , "The specified index is out of range" ) ) ;
  }
  return ( this.neighbor [ Index ] ) ;
  try
  {
  return ( this.neighbor [ Index ] ) ;
  }
  catch ( System.IndexOutOfRangeException )
  {
  throw ( new System.ArgumentOutOfRangeException ( "Index" , Index , "The specified index is out of range" ) ) ;
  }
  Arrays
  Can you elucidate please? Last time I looked there were only 64 voters.
  General News Suggestion Question Bug Answer Joke Praise Rant Admin
  Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.