dynamically resizing arrays

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

As there is no ReDim in C# , what is the preferred technique for dynmically
increasing the size of any array ?

Does the answer depend on whether the array holds primitives versus
instances of classes ?
 
As there is no ReDim in C# , what is the preferred technique for dynmically
increasing the size of any array ?

To use a List<T> or ArrayList instead, unless it has to be an actual
array. Otherwise you have to implement the array "resizing" yourself
by allocating a new array and copying the exisitng elements.


Mattias
 
Mattias Sjögren said:
To use a List<T> or ArrayList instead, unless it has to be an actual
array. Otherwise you have to implement the array "resizing" yourself
by allocating a new array and copying the exisitng elements.

Unfortunately a large number of framework methods take arrays as parameters.
Even more irritatingly, many of them don't have overloads to specify array
ranges which often forces you to make at least one "unnecessary" copy - in
which case you might as well use List<T>.ToArray() anyway.
 
For one dimensional arrays in C# 2, just use Array.Resize:
Array.Resize(ref YourArray, NewLength);
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
Back
Top