Reallocation

  • Thread starter Thread starter Jon Cosby
  • Start date Start date
J

Jon Cosby

Can someone suggest a way to reallocate an array without losing data?


Jon Cosby
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top