List <T> .AddRange / InsertRange create a temporary array
Looking at the implementation of List.AddRange, I found something strange, I donβt understand. Source code, see line 727 (AddRange calls InsertRange)
T[] itemsToInsert = new T[count]; c.CopyTo(itemsToInsert, 0); itemsToInsert.CopyTo(_items, index); Why do this? First, copy the collection to "temp-array" (itemsToInsert), and then copy the temp array to the actual _items array? Is there any reason for this, or is it just some kind of problem copying the source of the ArrayList, because the same thing happens there.
I guess this hides the existence of an internal support array. It is not possible to get a reference to this array, which is intentional. The List class does not even promise that such an array exists. (Of course, for performance and compatibility reasons, it will always be implemented using an array.)
Someone can pass the processed ICollection<T> , which remembers the array that it passed. Callers can now interact with the internal List array and start depending on the internal List elements.
Contrast this with a MemoryStream , which has a documented way to access the internal buffer (and shoot at it): GetBuffer() .