C#插入一个变量

Bla*_*fix -4 c#

我有这个代码,我有一个arrayList.我使用方法arrayList.Contains.

 if (arrayList2.Contains(5))
 {
     //something
 }
Run Code Online (Sandbox Code Playgroud)

我想在代码中用一个包含值arrayList2的变量(例如一个字符串)替换arrayList2.

string hello = "arrayList2" // create a varible with the value of the name of the arrayList
if (hello.Contains(5)) // insert the variable "hello" instead of "arrayList2"
{
    //something
}
Run Code Online (Sandbox Code Playgroud)

这种方法不起作用,任何想法我怎么能让它工作?

Sno*_*ear 7

我会试着猜一下这个.

也许你想要这样的东西:

Dictionary<string, ArrayList> arrayLists = new Dictionary<string, ArrayList>();
arrayLists.Add("arrayList1", new ArrayList());
arrayLists.Add("arrayList2", new ArrayList());
arrayLists.Add("arrayList3", new ArrayList());

string hello = "arrayList2";
if (arrayLists[hello].Contains(5)) { } 
Run Code Online (Sandbox Code Playgroud)