我创建了多个列表,并希望在拥有变量名称时使用每个单独的列表。
例如:
private List<string> listAlabama = new List<string>();
private List<string> listTexas = new List<string>();
// fill Lists with values
//Call Method to assign state to correct list
AssignNameToList("Alabama");
private void AssignNameToList(string state)
{
// Assign variable to the correct List e.g
sring tempFindList="list"+state;
//Use the right List
string tempVal=tempFindList[0]???
// should be listAlabama
}
以下是我使用字典提出的解决方案
var dictionary = new Dictionary<string, List<string>>();
dictionary.Add("Alabama", listAlabama);
dictionary.Add("Texas", listTexas);
switch (tempState)
{
case "Alabama":
List<string> value = dictionary["Alabama"];
Console.WriteLine(value);
string[] tempArray = value[totalPopulationIdx].Split(',');