I’m looking to do this in C#.
public struct Structure1
{ string string1 ; //Can be set dynamically
public string[] stringArr; //Needs to be set dynamically
}
In general, how should one initialize an array dynamically if need be?
In simplest of terms, I’m trying to achieve this in C#:
int[] array;
for (int i=0; i < 10; i++)
array[i] = i;
Another example:
string[] array1;
for (int i=0; i < DynamicValue; i++)
array1[i] = "SomeValue";
First off, your code will almost work:
You could potentially initialize your arrays within your struct by adding a custom constructor, and then initialize it calling the constructor when you create the struct. This would be required with a class.
That being said, I’d strongly recommend using a class here and not a struct. Mutable structs are a bad idea – and structs containing reference types are also a very bad idea.
Edit:
If you’re trying to make a collection where the length is dynamic, you can use
List<T>instead of an array: