tags:

views:

2731

answers:

4

I was shocked to find out today that c# does not support dynamic sized arrays, how then does a vb.net developer used to using redim preserve deal with this in C#?

at the beginning of the function I am not sure of the upper bound of the array, this depends on the rows returned from the database.

+6  A: 

Use a List<T>. It will dynamically size as needed.

driis
+1 never use ArrayList.
sixlettervariables
+8  A: 

Use ArrayLists or Generics instead

Michael L
Thank you , just googled Generics, feel a bit foolish because I didn't know these things existed
JL
More precisely, use List<T>. There are plenty of generic classes or functions in .NET that aren't resizeable arrays. ;)
jalf
I would stay away from ArrayLists. Besides being harder to use, Microsoft has said they won't be available on new platforms like Silverlight.
Jonathan Allen
+1  A: 

You really shouldn't be using ReDim, it can be every expensive. I prefer List(Of T), but there are many options in this area.

That said, you had a question and here is your answer.

x = (int[]) Utils.CopyArray((Array) x, new int[10]);
Jonathan Allen
How do you think that List<T> copes when it needs to resize its internal buffer? It does exactly the same as ReDim Preserve, basically... Why do you think ReDim is expensive, but not List<T>? (List<T> is certainly more convenient in many ways, but they basically need to do the same thing...)
Jon Skeet
Jon, Isn't ReDim Preserve adding what we need at the time we ask and List<T> doubling the size as .net thinks it's needed? I would assume this is monitored and doubled when the framework thinks it's optimum to do so and thus less expensive? Thanks!
We have to put things in context to see the differences between ReDim Preserve (actually Array.Copy) and List<T>.If you’re doing your resizing inside a loop, after a number of iterations, List<T> ends up allocating less memory than ReDim Preserve. That happens because while a new array is created for each loop iteration with Redim Preserve, with List<T> new arrays only are created when the current one does not have space for a new element.
Alfred Myers
+17  A: 

VB.NET doesn't have the idea of dynamically sized arrays, either - the CLR doesn't support it.

The equivalent of "Redim Preserve" is Array.Resize<T> - but you must be aware that if there are other references to the original array, they won't be changed at all. For example:

using System;

class Foo
{
    static void Main()
    {
        string[] x = new string[10];
        string[] y = x;

        Array.Resize(ref x, 20);
        Console.WriteLine(x.Length); // Prints out 20
        Console.WriteLine(y.Length); // Still prints out 10
    }
}

Proof that this is the equivalent of Redim Preserve:

Imports System

Class Foo
    Shared Sub Main()
        Dim x(9) as String
        Dim y as String() = x

        Redim Preserve x(19)
        Console.WriteLine(x.Length)
        Console.WriteLine(y.Length)
    End Sub
End Class

The two programs are equivalent.

If you truly want a dynamically sized collection, you should use List<T> (or something similar). There are various issues with using arrays directly - see Eric Lippert's blog post for details. That's not to say you should always avoid them, by any means - but you need to know what you're dealing with.

Jon Skeet
There is one difference between the techniques. Array.Resize works only on zero-based single dimensional arrays. Redim works on multi-dimensional arrays. No sure about the zero-based portion
JaredPar
Yup, that's true - thanks for pointing it out.
Jon Skeet