使用不同类型定义二维动态数组

Har*_*hna 7 .net c# windows

我想创建不同类型的二维数组,就像我可以添加到该数组中的两个值,其中一个是controlname,第二个是boolean值.

Kon*_*man 12

你不能这样做.相反,您应该创建一个包含这两个属性的类,然后您可以创建该类型的数组:

public class MyClass
{
    public string ControlName {get;set;}
    public bool MyBooleanValue {get;set;}
}

public MyClass[] myValues=new MyClass[numberOfItems];
Run Code Online (Sandbox Code Playgroud)

或者,正如Anders所说,如果其中一个属性用于执行查找,请使用字典.


mon*_*y_p 5

字典将适用于您尝试执行的操作.

Dictionary<string, bool> controllerDictionary = new Dictionary<string, bool>();
Run Code Online (Sandbox Code Playgroud)

设置值

if (controllerDictionary.ContainsKey(controllerName))
    controllerDictionary[controllerName] = newValue;
else
    controllerDictionary.Add(controllerName, newValue);
Run Code Online (Sandbox Code Playgroud)

获得价值

if (controllerDictionary.ContainsKey(controllerName))
    return controllerDictionary[controllerName];
else
    //return default or throw exception
Run Code Online (Sandbox Code Playgroud)