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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1939443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:02:14  来源:igfitidea点击:

Defining two dimensional Dynamic Array with different types

c#.netwindows

提问by Harikrishna

I want to create two dimension array of different type like I can add to that array two values one of them is controlname and second is boolean value.

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

采纳答案by Konamiman

You can't do that. Instead, you should create a class that contains these two properties, then you can create an array of that type:

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

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

public MyClass[] myValues=new MyClass[numberOfItems];

Or, as Anders says, use a dictionary if one of the properties is meant to be used to perform lookups.

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

回答by Anders Fjeldstad

If you want to lookup/set a boolean by control name, you could use a Dictionary<string, bool>.

如果要按控件名称查找/设置布尔值,可以使用Dictionary<string, bool>.

回答by Oded

You can't to that with an array.

你不能用数组来做到这一点。

Perhaps you should be using a Dictionary?

也许您应该使用Dictionary

A generic dictionary of Dictionary<string,bool>appears to be the kind of thing that will work for your description.

的通用字典Dictionary<string,bool>似乎适合您的描述。

回答by marcos

Use Dictionary<string,bool>. If, for some reason, you really need an array, try object[,] and cast its values to the types you want.

使用 Dictionary<string,bool>。如果出于某种原因,您确实需要一个数组,请尝试 object[,] 并将其值转换为您想要的类型。

回答by monkey_p

A dictionary will work for what you are trying to do then.

一本字典将适用于您当时正在尝试做的事情。

Dictionary<string, bool> controllerDictionary = new Dictionary<string, bool>();

To set a value

设置一个值

if (controllerDictionary.ContainsKey(controllerName))
    controllerDictionary[controllerName] = newValue;
else
    controllerDictionary.Add(controllerName, newValue);

To get a value

获取值

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

回答by WViviers

Another way of doing it is to create an array of type object, and then add this to an arraylist. Here is some sample code:

另一种方法是创建一个对象类型的数组,然后将其添加到数组列表中。下面是一些示例代码:

    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);
                }
            }
        }
    }

回答by Mike de Klerk

It depends on how you want to use your array. Do you want to look up the value by a key, or by its index? Konamiman suggested a class. But a class with two types is nothing more than a Dictionary<type of key, type of value>. You can use a dictionary if you want to obtain the value by a key. Like so:

这取决于您想如何使用您的阵列。你想通过一个键还是通过它的索引来查找值?Konamiman 推荐了一个班级。但是一个有两种类型的类只不过是一个Dictionary<type of key, type of value>. 如果要通过键获取值,可以使用字典。像这样:

Dictionary<string, int> MyDict = new Dictionary<string, int>();
MyDict.Add("Brutus", 16);
MyDict.Add("Angelina", 22);
int AgeOfAngelina = MyDict["Angelina"];

Now the disadvantage of a dictionary is that, you can't iterate over it. The order is undetermined. You can not use MyDict[0].Valueto obtain the age of Brutus (which is 16).

现在字典的缺点是,你不能迭代它。顺序未定。您不能使用MyDict[0].Value来获取布鲁图斯的年龄(即 16 岁)。

You could use a

你可以使用一个

List<KeyValuePair<string, int>> MyList = new List<KeyValuePair<string, int>>();

to iterate through a 2D array of two different types as a Listsupports iteration. But then again, you cannot obtain the age of Angelina by MyList["Angelina"].Valuebut you would have to use MyList[0].Value.

迭代两种不同类型的二维数组作为List支持迭代。但是话又说回来,您无法通过 获得安吉丽娜的年龄,MyList["Angelina"].Value但您必须使用MyList[0].Value.

But you could use a datatable as well. But it requires somewhat more work to initialize the table with its columns.

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

回答by Hatim

"A multi-dimensional array is an array: all elementsin all dimensions have the same type"

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