尝试将元素添加到List <string [,]> S = new List <string [,]>(); </string [,]> </string [,]>

时间:2011-07-27 06:09:07

标签: c# string collections

我正在尝试将Elements添加到以下内容中。它不起作用。我该怎么办?

List<String[,]> S = new List<String[,]>();

告诉你为什么我要尝试这样的事情; 我最初需要以下::

        String[,] s = new String[60,2] ;
        s[0,0] = ".NET CLR LocksAndThreads";
        s[0,1] = "Contention Rate / sec";
        s[1,0] = "ASP.NET Applications";
        s[1,1] = "Requests Rejected";
        s[2,0] = "Memory";
        s[2,1] = "Available Mbytes";
        s[3,0] = "Process";
        s[3,1] = "Private Bytes";
        s[4,0] = "Network Interface";
        s[4,1] = "Bytes Received/sec";

但后来我想为什么不使用List。所以请告诉我我做错了什么..

2 个答案:

答案 0 :(得分:6)

您似乎希望将2D字符串数组转换为对您的给定数据更有意义的内容。

查看2D数组中的这些值,并根据它是一个X by 2数组的事实,使用字典可能更有意义:

Dictionary<string, string> S = new Dictionary<string, string>
{
    { ".NET CLR LocksAndThreads", "Contention Rate / sec" }, 
    { "ASP.NET Applications", "Requests Rejected" }, 
    { "Memory", "Available Mbytes" }, 
    { "Process", "Private Bytes" }, 
    { "Network Interface", "Bytes Received/sec" }
};

答案 1 :(得分:3)

好像你想存储成对的字符串。如果每对中的第一个字符串是唯一的(我怀疑它是),那么Dictionary将为您执行此操作。

e.g。

var dictionary = new Dictionary<string, string>
    {
        { "a", "x" },
        { "b", "y" },
    }

如果每对中的第一个字符串不是唯一,那么您可以使用KeyValuePair的集合。

var list = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("a", "x"),
        new KeyValuePair<string, string>("b", "y"),
    }