I know this is a simple question, but it had been bothering me for a whole day long. I try to set the length of my array through a variable inside a method(setLength). But after i call the method setLength, the length of my array still 0, doesn’t it was set to 5 when i calling the method setLength?
C# code snippet:
class myProject
{
public static int length = 0;
public static string[] myArray1 = new string[length];
public static string[] myArray2 = new string[length];
public static void setLength()
{
length = 5;
}
public static void process()
{
setLength();
for(int i=0; i<length; i++)
{
.....code for my array
}
}
You have to create a new array.
You can’t just change the length at will. c# arrays don’t support that.
If you want to use a variable-length structure, I’d suggest using a
List<string>