Dynamic Arrays?

FlyBoy

Centurion
Joined
Sep 6, 2004
Messages
106
is there any way to use dynamic arrays in c# except from collections (arraylist...)??
when im trying to perform the following action:
int[] ar = new int[] /// Not bounded.

i get an error....
 
FlyBoy said:
is there any way to use dynamic arrays in c# except from collections (arraylist...)??
when im trying to perform the following action:
int[] ar = new int[] /// Not bounded.

i get an error....

you need to specify the values of the array if when you initialize it like that
e.x.:
int[] ar = new int[] {1,2,4,8}
//The length of the array will be the # of values entered (4), and will contain the values 1,2,4,8

int[] ar = new int[6];
array with a length of 6, but all the values are zero.
dynamic arrays can also be made using Array.CreateInstance.
 
i believe the reason for not allowing a redim of arrays in C# is that they wouldn't be self describing which makes them not remotable.

The array needs to be redefined.

why do you not want to use the array list???
 
Joe Mamma said:
i believe the reason for not allowing a redim of arrays in C# is that they wouldn't be self describing which makes them not remotable.

The array needs to be redefined.

why do you not want to use the array list???

If I were to guess, maybe because arraylists aren't strongtyped and .net 2.0 final hasn't been released yet for generics.
 
You can VERY easily do a "preserve" yourself, if it's what you need:
C#:
int[] a = new int[] {1, 2, 3, 4, 5};
int[] b = new int[10]; // the array to preserve "to"
a.CopyTo(b, 0);

If this is in a function, just return b. If you need the array back in a, just assign a to b:
C#:
a = b;

As a note: Generally, you won't need to preserve an array. If you really need to keep resizing an array, you're probably better off with ArrayList and converting to a true array when you're done (if that's even needed). If you're resizing in a loop, you likely know the size of the array to begin with and you can just set the size of the array once, before getting in the loop.

The "Preserve" keyword in VB works like the code posted above. That is, it's *copying* the array every time. If you do this a lot on big arrays, that's a LOT of extra overhead.

-ner
 
Joe Mamma said:
and now that I think about it, array lists aren't firectly remotable either.

ArrayLists should be remoteable, it has the serializable tag. It would probably fail @ runtime though, if it contains a non-serializable object.
 
ok i see.....thanks for the help guys. really thanks.
at least now i know that im not missing anything
 
Back
Top