2011-07-27 68 views
-2

我想将元素添加到以下内容。它不工作。我该怎么做 ?试图将元素添加到列表<String[,]> S = new List <String[,]>();

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"; 

但转念一想,为什么不使用名单。所以请告诉我我做错了什么..

+6

“不工作”在尝试排除故障时是完全没有用的信息。可能想重新修改你的问题。 –

+0

首先,我不明白你是如何添加元素的。 – BoltClock

回答

6

您似乎想要将二维字符串数组转换为您的给定数据更有意义的东西。

看在你的二维数组的值,并基于这样的事实,它是由2阵列的X,它可能更有意义使用字典:

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" } 
}; 
3

好像你想存储双串。如果每对中的第一个字符串是唯一的(我怀疑它是这样),那么Dictionary会为你做这个。

例如

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"), 
    } 
+0

关于字符串/键的唯一性的好处... – BoltClock

相关问题