Reallocation

  • Thread starter Thread starter Jon Cosby
  • Start date Start date
Hi, Jon

In C# I usually create new array and Copy / AddRange elements from old one.
If array is too big possible better would be to use ArrayList, which allows
to add/ remove elements dynamically.

HTH
Alex
 
Jon Cosby said:
Can someone suggest a way to reallocate an array without losing data?

You can't "reallocate" an array. All you can do is create a new array
and copy the data from one to the other using Array.Copy.
 
AlexS said:
In C# I usually create new array and Copy / AddRange elements from old one.
If array is too big possible better would be to use ArrayList, which allows
to add/ remove elements dynamically.

ArrayList does exactly the same thing under the covers, of course. It
just doesn't do it on every change - it has an array which is bigger
than needed (usually) and which gets filled until it can no longer hold
everything required, at which point the creation/copy takes place.
 
Back
Top