Declaring variable sized array in C#
-
Sunday, March 25, 2007 3:43 PM
I an used to declaring a variable sized array in other languages using the following syntax
int myarray[ ];
Looks like in C# I have to specify a size. What if I do not know this size until run time, how do I declare such an array
Thanks in advance.
Klaus
All Replies
-
Sunday, March 25, 2007 4:00 PMModerator
In C# the syntax is slightly different:
int[] myarray = null;
// ...
int arraySize = 1;
// ...
myarray = new int[arraySize]; -
Sunday, March 25, 2007 6:24 PM
I'd probably use an instance of List<int> instead.. This way my collection can grow/shrink/whatever... And everytime i need an array representation of the collection i call ToArray()... -
Sunday, March 25, 2007 7:00 PMThanks guys.
-
Monday, March 26, 2007 4:10 AMModerator
For more information about generics in C#, see: http://msdn2.microsoft.com/en-us/library/0x6a29h6.aspx
Thanks

