Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用不同类型定义二维动态数组_C#_.net_Windows - Fatal编程技术网

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

C# 用不同类型定义二维动态数组,c#,.net,windows,C#,.net,Windows,我想创建不同类型的二维数组,就像我可以向该数组添加两个值一样,其中一个是controlname,第二个是boolean值。如果要按控件名称查找/设置布尔值,可以使用。不能将其与数组一起使用 也许你应该使用一种新的方法 一本字典的通用字典似乎适合你的描述。你不能这样做。相反,您应该创建一个包含这两个属性的类,然后可以创建该类型的数组: public class MyClass { public string ControlName {get;set;} public bool My

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

如果要按控件名称查找/设置布尔值,可以使用。

不能将其与数组一起使用

也许你应该使用一种新的方法


一本
字典的通用字典
似乎适合你的描述。

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

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

public MyClass[] myValues=new MyClass[numberOfItems];
或者,正如Anders所说,如果其中一个属性用于执行查找,则使用字典。

使用字典。
如果出于某种原因,您确实需要一个数组,请尝试对象[,]并将其值强制转换为您想要的类型。

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

Dictionary<string, bool> controllerDictionary = new Dictionary<string, bool>();
获得价值

if (controllerDictionary.ContainsKey(controllerName))
    controllerDictionary[controllerName] = newValue;
else
    controllerDictionary.Add(controllerName, newValue);
if (controllerDictionary.ContainsKey(controllerName))
    return controllerDictionary[controllerName];
else
    //return default or throw exception

另一种方法是创建object类型的数组,然后将其添加到arraylist中。 以下是一些示例代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    using System.Collections.Generic;

    namespace Collections
    {
        class Program
        {
            static void Main(string[] args)
            {
                ArrayList ar = new ArrayList();
                object[] o = new object[3];
                // Add 10 items to arraylist
                for (int i = 0; i < 10; i++)
                {
                    // Create some sample data to add to array of objects of different types.
                    Random r = new Random();
                    o[0] = r.Next(1, 100);
                    o[1] = "a" + r.Next(1,100).ToString();
                    o[2] = r.Next(1,100);
                    ar.Add(o);
                }
            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统集合;
使用System.Collections.Generic;
命名空间集合
{
班级计划
{
静态void Main(字符串[]参数)
{
ArrayList ar=新的ArrayList();
对象[]o=新对象[3];
//向arraylist添加10项
对于(int i=0;i<10;i++)
{
//创建一些示例数据以添加到不同类型的对象数组中。
随机r=新随机();
o[0]=r.Next(1100);
o[1]=“a”+r.Next(1100).ToString();
o[2]=r.Next(1100);
ar.Add(o);
}
}
}
}

这取决于您希望如何使用阵列。您想通过键或其索引查找值吗?科纳米曼建议上课。但是有两种类型的类只不过是一个
字典
。 如果要通过键获取值,可以使用字典。 像这样:

列表的形式迭代两种不同类型的2D数组
支持迭代。但同样,您无法通过
MyList[“Angelina”]获得安吉丽娜的年龄。Value
但您必须使用
MyList[0]。Value

但您也可以使用数据表。但初始化表及其列需要更多的工作。

“多维数组是一个数组:
所有维度中的所有元素都具有相同的类型“

我不确定我是否理解您的问题,但字典对您的工作不起作用吗?您正在尝试存储键/值对吗?使用具有元组的列表
public static list pixelsArr=new list()
您可以只使用
controllerDictionary[controllerName]=newValue
ContainsKey
Add
是不需要的Ye,没错,它只需要用于Getthe。当然,您可以使用LINQ进行扩展以使用对数据的查询。从什么时候开始,您就不能迭代字典了?
Dictionary<string, int> MyDict = new Dictionary<string, int>();
MyDict.Add("Brutus", 16);
MyDict.Add("Angelina", 22);
int AgeOfAngelina = MyDict["Angelina"];
List<KeyValuePair<string, int>> MyList = new List<KeyValuePair<string, int>>();