Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I guarantee that a non-ref method doesn't change the array size C#

Tags:

arrays

c#

See also:

  • change array size
  • Resize array in C# later in the program

Both of those questions are different in that they ask HOW you would change the size, to which I'm well aware that the answer is: "Don't ... use a list". They also reference a ref-based Array.Resize(ref array, int size).

My question is this:

Given that I have a monolithic legacy codebase, that I can't possibly examine the entirety of, and can't make any guarantees of what it does, ... if I have call method passing in a not-by-ref array, can I be guaranteed that the size of the array afterwards is unchanged.

Array.Resize isn't an issue, since the by-ref-ness would have to pass all the way up through the call stack, which it obviously doesn't.

But are there any other possible issues?

REMARKS ON NOT BEING A DUPLICATE

I'd argue that this is a different question from the others, because this is an "Is it possible?" rather than "how would I do it/what's the best way to do it". Thus answers that would be evil and wrong and would not be mentioned in the latter, would still be relevant to my question.

like image 325
Brondahl Avatar asked Nov 24 '16 14:11

Brondahl


1 Answers

can I be guaranteed that the size of the array afterwards is unchanged.

Yes, because arrays never change size. Ever. Array.Resize creates a new array and copies the data; it does not actually resize anything. If you have a reference to the old array, then: you're fine and the size is unchanging.

like image 94
Marc Gravell Avatar answered Sep 26 '22 20:09

Marc Gravell