David Anton said:
Although ArrayList may be preferable in some cases, you don't need to choose
it just because you want to resize easily. Arrays are also easily resized.
With C# 2.0 simply use Array.Resize (for one-dimensional arrays).
You need to be careful here. Arrays can't be resized in the same way
that Strings can't be changed. Array.Resize doesn't *actually* resize
the array any more than String.Replace replaces occurrences in the
string it's called on. Instead, Array.Resize returns a *new* array of
the desired size, with the elements from the original array copied into
it. Now, if you've only got one reference to the array, that's fine -
but if you've got two variables which refer to the original array,
calling Array.Resize will have no effect as far as the "extra" variable
is concerned. Here's an example:
using System;
class Test
{
static void Main()
{
string[] x = {"hello", "there"};
string[] y = x;
Array.Resize(ref x, 3);
Console.WriteLine (x.Length);
Console.WriteLine (y.Length);
}
}
(I'm sure you knew all this David - I just wanted to be clear for the
OP.)