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.
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.
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.
You can VERY easily do a "preserve" yourself, if its 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 wont need to preserve an array. If you really need to keep resizing an array, youre probably better off with ArrayList and converting to a true array when youre done (if thats even needed). If youre 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, its *copying* the array every time. If you do this a lot on big arrays, thats a LOT of extra overhead.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.