declare array without length

goddesso

Newcomer
Joined
Feb 21, 2004
Messages
2
Location
Ohio
Is there any way to declare an array without initially declaring the size? I am trying to use a 2 dimentional array that can add elements during user interaction and keep the existing elements in tact. I also need to be able to access the last element in the array, which is why I can't declare a size.

int [][] array = new int [5][];
for loop.....
array[0][new index].Add(element);
for loop....
array[1][new index].Add(element);

I need to be able to access the last index in this array which is unknown, but I can't add new elements unless the size is declared. Can anyone help? I am not very experienced with C#, and cannot find the conversion from VB to fix this. Thanks to anyone who can help.
 
Here's an example I produced by using Instant C# (the vb.net to c# converter):

VB Code:
Dim YourArray(,) as integer
redim preserve YourArray(5, 0)
YourArray(0, 0) = 2

C# Code:
int[,] YourArray = null;
//INSTANT C# NOTE: The following 4 lines reproduce what 'ReDim Preserve' does behind the scenes in VB.NET:
//ORIGINAL LINE: redim preserve YourArray(5, 0)
int[,] Temp1 = new int[6, 0 + 1];
if (YourArray != null)
System.Array.Copy(YourArray, Temp1, YourArray.Length);
YourArray = Temp1;
YourArray[0, 0] = 2;
 
Back
Top