Resize array in C# later in the program


Resize array in C# later in the program



I am not sure if this is possible in c#
but i'm having a variable defined in


public partial class Form1 : Form
{...
...
#region used variables
public ulong logP = {0,0,0,0,0,0,0,0}
....
..



Then later in the program i want have an option to adjust the size in the program
before the main routines will launch over it and do some calculations



Because i like to test with vaious sets of numbers and array sizes i would like to have an option to resize the array, for each test (but this array isnt bound to a single procedure, its a global variable that needs to be resizable.



As the whole program at various parts is using this array, (under various functions) how should i put this part


ulong sumP = new ulong[numericupdown.valeu];



So that it would change this global variable size ?





why dont you use a List<ulong>?
– Daniel A. White
Oct 11 '13 at 22:29


List<ulong>





Consider using a List instead of an array.
– Icemanind
Oct 11 '13 at 22:30


List





Arrays are not resizable. That is all.
– Ed S.
Oct 11 '13 at 22:46





arrays can be multidimensional while lists can not, and in my case i use some quite complex math, thats the reason i dont use lists. sure in the end i could have reworked it all and change the program to work with lists, but that would require to much changes.
– user613326
Apr 17 '14 at 18:31





4 Answers
4



You cannot resize an array; you must declare a new array of the desired size and copy the contents of the original array into the new array.



Update: I don't like Array.Resize - it doesn't resize the array (as the method name would suggest), it creates a new array and replaces the reference:


Array.Resize


static void Main(string args)
{
int a1 = new int { 1, 2, 3 };
int a2 = a1;

Console.WriteLine("A1 Length: {0}", a1.Length); // will be 3
Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3
Array.Resize(ref a1, 10);
Console.WriteLine("A1 Length: {0}", a1.Length); // will be 10
Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3 - not good

Console.ReadLine();
}





Refer to Array.Resize in the MSDN documentation.
– staafl
Oct 11 '13 at 22:32


Array.Resize





Interesting, I never knew about that - it's just a shortcut to what I described, however, and leaves any other references to the original array the same as described in my update
– Moho
Oct 11 '13 at 22:38





in other words - you're not resizing the array (regardless of the name of the method), you're replacing the array.
– Moho
Oct 11 '13 at 22:39





fair enough, un-downvoted :-)
– staafl
Oct 11 '13 at 22:40





+1 for proving that you'll get a new instance from the (ref x) keyword. And the original array isn't resized. This shows that an array it self cannot be resized (same like alloc/mallocs). The only thing you'll think it is resizing, because it copies the contents also. I would like to know why the other downvoted it... :)
– J. van Langen
Oct 11 '13 at 22:44



class Program
{
static void Main(string args)
{
var myArr = new int[5] { 1, 2, 3, 4, 5 };
Array.Resize(ref myArr, 10);
for (int i = 0; i < myArr.Length; i++)
{
Console.WriteLine(" [{0}] : {1}", i, myArr[i]);
}
Console.ReadLine();
}
}



as icemanind suggested - consider using a List array however as better practice, make sure you initialize the array capacity to a reasonable amount i.e:


List<string> myList = new List<string>(10); // initial capacity is 10



the advantage is performance and space allocation is predefined and it doesn't mean it needs to recreate the collection everytime you add an item to it as it would use the existing allocated amount until its reached then it increases it internally.





"initial length is 10" No thats the capacity, the length is zero.
– J. van Langen
Oct 11 '13 at 22:39





right, that's what I meant - sorry for the confusion!
– Ahmed ilyas
Oct 12 '13 at 11:37



Use Array.Resize() to resize an array,


Array.Resize()



http://msdn.microsoft.com/en-us/library/bb348051.aspx



For example:


var intArray = new int[5] { 1, 2, 3, 4, 5 };

Array.Resize(ref intArray, 10);





See my downvoted answer below to see why this doesn't resize an array
– Moho
Oct 11 '13 at 22:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift