The problem is that you're using the non-generic ArrayList
type - so the compile-time type of arraysNames[i1]
is object
, not string[]
. You should almost never use ArrayList
in modern code - since 2005, the preferred generic equivalent has been List<T>
. So this code will compile:
public static string[] traitNames = { "Happiness", "Respect", "Authority" };
private List<string[]> arraysNames = new List<string[]> { traitNames, suppliesNames };
// Later in code
string[] currentArrayNames = arraysNames[i1];
Note that this doesn't create a new array - it just uses the existing one. I'm assuming that's what you wanted, really.
If you absolutely can't change the type of arraysNames
, you can just cast instead:
string[] currentArrayNames = (string[]) arraysNames[i1];
It's definitely better to use List<string[]>
instead though.
As a side-note, I'd strongly recommend avoiding making fields public as you have with traitNames
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…