Array cannot resize.
- Managed arrays never change in size:
An array in C # is a "managed array" that is allocated by GC on a managed heap. In Managed C ++, you should write [1] :
array<N*>^ arr = gcnew array<N*>(3);
This is a single atomic object from the point of view of a managed heap, and it cannot be changed, only and ultimately collected garbage is used. The operation gcresize or gc-re-new in managed C ++ [2] is absent because it is forbidden in the CLR. This will greatly complicate managed GCs if managed objects can change in size over their lifetime. I could not find the final document for this statement, but I'm pretty sure.
You may ask how the " Array.Resize " method works if arrays never change size. As the documents explain :
This method selects a new array with the specified size, copies the elements from the old array to the new one, and then replaces the old array with the new one.
The Array.Resize method Array.Resize updates the typed array variable to point to the new array (because the variable is pass-by-ref), and allows the old array to be GC'ed (if there is no other ref to it).
- Method arguments in C # are pass-by-val
... therefore, unless you use any other by-ref methods, no one can update the array variable of the array to point to another array.
I believe that 1 and 2 together will answer your question.
How is this different from the list?
A List can be resized without using the by-ref method. This is a little different. The List object itself is in a managed heap and never changes in size, but it contains a pointer to an array as a member variable. When the List grows, this pointer is updated, pointing to a longer array containing new elements.
source share