Can I guarantee that the non-ref method will not resize the C # array

See also:

Both of these questions differ from each other in that they ask how you would resize, which I know very well that the answer is: "Don't ... use a list." They also reference ref-based Array.Resize(ref array, int size) .

My question is:

Given that I have a monolithic obsolete code base that I cannot fully verify, and I can not guarantee any guarantees what it does ... if I have a call method that goes into a non-ref array, can I ensure that the size of the array does not subsequently change.

Array.Resize is not a problem, since by-refess will need to go all the way through the call stack, which obviously is not.

But are there other possible problems?

Notes on not using DUPLICATE

I would say that this is a different question from others, because it is "is it possible?". not "how do I do it / what’s the best way to do this." Thus, answers that would be evil and wrong and not mentioned in the latter will still be relevant to my question.

+5
source share
2 answers

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).

  1. 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.

+1
source

Is it possible to guarantee that the size of the array will not change subsequently.

Yes, because arrays never resize. Ever. Array.Resize creates an array new and copies the data; it does not actually resize everything. If you have a reference to the old array, then: you are fine, and the size is unchanged.

+20
source

Source: https://habr.com/ru/post/1260281/


All Articles